[英]Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840
I am working on swift project and calling webservice with Alamofire.我正在开发 swift 项目并使用 Alamofire 调用 webservice。 But, while calling post method, I am getting following error.
但是,在调用 post 方法时,出现以下错误。
Header file :
let accessTokenHeaderFile = [
"Accept": "application/json",
"Content-Type" :"application/json",
"X-TOKEN" : UtilityClass.sharedInstance.accessTokenString
]
Alamofire.request(urlString, method: .post, parameters: params as? [String:Any], encoding: JSONEncoding.default, headers: accessTokenHeaderFile).responseJSON { response in
requestVC.removeLoader()
switch (response.result) {
case .success:
if response.result.value != nil{
completionHandler (response.result.value)
}
break
case .failure(let error):
failureHandler (error as NSError?)
break
}
}
And the error is错误是
FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
Can anyone suggest me, how to fix this, I tried googling, but whatever I found the answers not helped me.任何人都可以建议我,如何解决这个问题,我尝试使用谷歌搜索,但无论我发现什么答案都没有帮助我。
Error of 3840 saying that the response from server is not a valid JSON string. 3840 错误,表示来自服务器的响应不是有效的 JSON 字符串。 So you can check you parameters key value may be it's wrong assign because similar of responseString instead of responseJSON .
所以你可以检查你的参数键值可能是错误的分配,因为类似于 responseString 而不是 responseJSON 。
Your response is not a valid json hence you're getting this error.您的回复不是有效的 json,因此您收到此错误。 Please check the
response.response?.statusCode
to see what is server returning.请检查
response.response?.statusCode
以查看服务器返回的内容。 And if you want to see the actual response try using responseString
or responseData
methods instead of responseJSON
如果您想查看实际响应,请尝试使用
responseString
或responseData
方法而不是responseJSON
eg例如
Alamofire.request(urlString, method: .post, parameters: params as? [String:Any], encoding: JSONEncoding.default, headers: accessTokenHeaderFile). responseData {
You can find out more response methods here您可以在此处找到更多响应方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.