Alamofire POST 方法不会将值传递给 url
Posted
技术标签:
【中文标题】Alamofire POST 方法不会将值传递给 url【英文标题】:Alamofire POST method doesn't pass value to url 【发布时间】:2016-11-18 06:01:06 【问题描述】:我在我的应用程序中使用 Alamofire 进行网络连接。在 Alamofire 中,我正在编写 POST 方法来将值传递给 url,但它没有传递任何值。显示为空。以下是我的代码:
let parameters = ["username": "\(textField.text)"]
let urlString = "**Some URL**"
Alamofire.request(.POST, urlString, parameters: parameters, encoding: .JSON).responseJSON response in
guard response.result.error == nil else
print("error calling POST on email API")
print(response.result.error!)
return
if let value = response.result.value
print("The email is: \(value)" )
如果有人弄错了我做错了什么。谢谢!!
【问题讨论】:
您使用的是 swift 2.x? 是的!我正在使用 swift 2.3。 会有什么反应?您是从服务器还是字符串获取 json 对象?验证! 电子邮件是: results = ".JSON
作为编码。保持为无或默认
【参考方案1】:
您使用的是哪个版本的 alamofire? 请尝试使用 URLEncoding.default 而不是 JSONEncoding.default。
【讨论】:
赞成。已经为这个问题挠头好几个小时了。【参考方案2】:试试这样:
让 url = NSURL(string:URLName)
让请求 = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
让数据 = 试试! NSJSONSerialization.dataWithJSONObject(参数!,选项:NSJSONWritingOptions.PrettyPrinted)
让 json = NSString(data: data, encoding: NSUTF8StringEncoding) 如果让 json = json 打印(json)
request.HTTPBody = json!.dataUsingEncoding(NSUTF8StringEncoding);
Alamofire.request(request)
.responseString response in
// do whatever you want here
print(response.result)
switch (response.result)
case .Success(let JSON):
print(JSON)
case .Failure: break
【讨论】:
这不是responseString
的问题。这是编码的问题。
完全不是这样的。alamofire 有几乎所有类型的编码参数,从 Json 到查询字符串到形成数据【参考方案3】:
我正在使用 swift2.3,我使用 Alamofire.. 它对我有用:
class APIUtils: NSObjectstatic func fetchData(requestModel:RequestModel,completionBlock:(response:ResponseModel)->Void)
let url = "\(requestModel.baseUrl)\(requestModel.apiUrl)"
Alamofire.request(APIUtils().getHTTPMethod(requestModel.httpMethodType), url, parameters: requestModel.parametersDict,headers:requestModel.headersDict,encoding:(APIUtils().getEncodingFormat(requestModel.encodeType))).responseJSON (responseObj) -> Void in
let responseModel = ResponseModel()
responseModel.responseCode = responseObj.response?.statusCode
switch responseObj.result
case .Success(let data):
if (data .isKindOfClass(NSDictionary))
responseModel.responseDictionary = data as! NSDictionary
else if (data .isKindOfClass(NSArray))
responseModel.customModel = data as! NSArray
responseModel.isSuccess = true
break
case .Failure(let error):
responseModel.isSuccess = false
print("Request failed with error: \(error)")
break
completionBlock(response: responseModel)
func getHTTPMethod(methodType:HTTPMethodType)->Alamofire.Method
switch(methodType)
case .GET: return Alamofire.Method.GET
case .POST: return Alamofire.Method.POST
case .PUT: return Alamofire.Method.PUT
case .PATCH: return Alamofire.Method.PATCH
case .DELETE: return Alamofire.Method.DELETE
func getEncodingFormat(methodType:encodingType)->Alamofire.ParameterEncoding
switch(methodType)
case .URL: return Alamofire.ParameterEncoding.URL
case .JSON: return Alamofire.ParameterEncoding.JSON
case .URLEncodedInURL: return Alamofire.ParameterEncoding.URLEncodedInURL
//试试这个:
let urlencode = Alamofire.ParameterEncoding
print(MyStrings.BaseURl+urlString)
Alamofire.request(action, MyStrings.BaseURl+urlString, headers: headers, parameters:urlParams, encoding: isBody == true ? urlencode.JSON : urlencode.URLEncodedInURL)
.validate(statusCode: 200..<300)
.responseJSON responseO in
if (responseO.result.error == nil)
let responseModel = ResponseModel()
responseModel.responseCode = responseO.response?.statusCode
switch responseO.result
case .Success(let data):
if (data .isKindOfClass(NSDictionary))
responseModel.responseDictionary = data as! NSDictionary
else if (data .isKindOfClass(NSArray))
responseModel.customModel = data as! NSArray
responseModel.isSuccess = true
break
case .Failure(let error):
responseModel.isSuccess = false
print("Request failed with error: \(error)")
break
completionBlock(response: responseModel)
else
debugPrint("HTTP Request failed: \(responseO.result.error?.userInfo)")
let responseModel = ResponseModel()
responseModel.responseCode = responseO.response?.statusCode
responseModel.isSuccess = false
completionBlock(response: responseModel)
【讨论】:
以上是关于Alamofire POST 方法不会将值传递给 url的主要内容,如果未能解决你的问题,请参考以下文章
邮递员插件将值[FromBody]传递给发布方法时的空值[重复]
React useContext 不会将值传递给深层嵌套的子级