我发现一些线索存在同样的问题,但他们的答案对我不起作用。
我有一个post
请求来检查用户登录信息。
网址格式:
“http://support.xxx.xx:8031/serpapi/login/checkLogin”
参数:
["contextInfo" :
["clientId": "1000000", "orgId": "1000001",
"warehouseId": "1000002", "roleId": "0"],
"userName": "hanoiaUser", "password": "hanoiaUser"]
请求:
Alamofire.request(url!, method: .post, parameters: params).responseJSON { (response) in
print(response)
}
错误回复:
FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
为什么我收到此错误?以及如何解决?
编辑:我尝试使用POSTMAN并返回响应:
Content-Type:application / json
身体:生 - json
{
"contextInfo" : {
"clientId":1000000,
"orgId": 1000001,
"warehouseId": 1000002,
"roleId": 0
},
"userName": "hanoiaUser",
"password": "hanoiaUser"
}
回应:
{
"success": true,
"data": [
{
"userId": 1000003,
"userName": "hanoiaUser",
"token": "b7e804d25065e5c3ac97d765180b7986"
}
],
"error": null
}
答案 0 :(得分:3)
好的,最后我解决了问题
这是新请求:
Alamofire.request(url!, method: .post,encoding : JSONEncoding.default, headers: headers, parameters: params).responseJSON { (response) in
print(response)
}
使用标题:
let headers = [
"Content-Type": "application/json"
]
答案 1 :(得分:1)
设置请求标头,如: request.allHTTPHeaderFields = [“Content-Type”:“application / json”]