Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域= NSCocoaErrorDomain代码= 3840
问题描述:
我正在研究迅速的项目,并使用Alamofire调用网络服务。
但是,在调用post方法时,出现以下错误。
I am working on swift project and calling webservice with Alamofire. But, while calling post method, I am getting following error.
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
}
}
错误为
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.
答
错误3840表示响应来自服务器不是有效的JSON字符串。因此,您可以检查 parameters键值可能是分配错误的原因,因为 responseString类似于responseJSON 。
Error of 3840 saying that the response from server is not a valid JSON string. So you can check you parameters key value may be it’s wrong assign because similar of responseString instead of responseJSON.