简体   繁体   English

Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域=NSCocoaErrorDomain 代码=3840

[英]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如果您想查看实际响应,请尝试使用responseStringresponseData方法而不是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.

相关问题 错误域= NSCocoaErrorDomain代码= 3840 - Error Domain=NSCocoaErrorDomain Code=3840 错误域=NSCocoaErrorDomain 代码=3840 JSONObject - Error Domain=NSCocoaErrorDomain Code=3840 JSONObject 错误域= NSCocoaErrorDomain代码= 3840在iOS迅速? - Error Domain=NSCocoaErrorDomain Code=3840 in iOS swift? APNS:错误域= NSCocoaErrorDomain代码= 3840 - APNS :Error Domain = NSCocoaErrorDomain Code=3840 AFNetworking 2.0中的Domain = NSCocoaErrorDomain代码= 3840错误 - Domain=NSCocoaErrorDomain Code=3840 error in afnetworking 2.0 错误 = 错误域 = NSCocoaErrorDomain 代码 = 3840 “垃圾结束。” - error = Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." 错误域= NSCocoaErrorDomain代码= 3840“字符周围的无效值 - Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 在 Swift5 中解析 JsonString 时出现错误 Domain=NSCocoaErrorDomain Code=3840 - Error Domain=NSCocoaErrorDomain Code=3840 When JsonString parsing in Swift5 仅在iPhone 5上带有错误域= NSCocoaErrorDomain代码= 3840的评估JavaScript()-iOS 10.3.3 - evaluateJavaScript() with Error Domain=NSCocoaErrorDomain Code=3840 just on iPhone 5 - iOS 10.3.3 Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) - Alamofire: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM