Ios 阿拉莫菲尔4.3罐';t发送JSON请求
我正在尝试使用Alamofire发送JSON请求,但它不能作为JSON请求工作这是我的JSON:Ios 阿拉莫菲尔4.3罐';t发送JSON请求,ios,json,swift,alamofire,Ios,Json,Swift,Alamofire,我正在尝试使用Alamofire发送JSON请求,但它不能作为JSON请求工作这是我的JSON: {"ClientID":"55050","AdminId":"myemail","Password":"123"} content-type → application/json; charset=utf-8 这是我的代码: import Alamofire func doLogin(username:String ,password:String) { let parameters
{"ClientID":"55050","AdminId":"myemail","Password":"123"}
content-type → application/json; charset=utf-8
这是我的代码:
import Alamofire
func doLogin(username:String ,password:String) {
let parameters = ["ClientID":"55050" , "AdminId":username,"Password":password]
Alamofire.request("myurl.com", method: .post, parameters: parameters, encoding: JSONEncoding(options: []) ).responseJSON(completionHandler: {
response in
print(response)
})
这是我得到的回应
失败:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域=NSCOCAERRORDOMAIN代码=3840“字符3周围的无效值”。.UserInfo={NSDebugDescription=字符3周围的无效值。}))
通过阅读文档,我只需要将请求的标题设置为application/json
所以我添加了额外的参数头:
let headers: HTTPHeaders = [
"Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
"Accept": "application/json"
]
Alamofire.request(ApiKeys().login, method : .post , parameters : parameters, encoding: JSONEncoding.default , headers: headers).responseJSON { response in
}
现在它工作得很好,谢谢大家的帮助:)看来您的请求中一定漏掉了标题。因此,没有任何回应。 标题必须是字典
[String:String]
let header = ["Content-Type" : "application/json"]
您的样本请求应该是:
Alamofire.request(getCategoryPath, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: header)
.responseJSON { response in
guard response.result.error == nil else {
print(response.result.error!)
}
print(response.result.value)
}根据错误,问题出在您服务器的响应上。我与邮递员一起尝试过,它正常工作