(Cocoa error 3840.)" (Invalid value around character 0.) AFNetworking

后端 未结 3 775
南方客
南方客 2020-12-29 04:14

I\'ve been getting the following error when using the GET method to retrieve a file from a server:

Error: Error Domain=NSCocoaErrorDomain Code=3840 \"The ope         


        
相关标签:
3条回答
  • 2020-12-29 04:35

    Judging by the discussion in the comments it appears that your GET request is successful (response code 200), but the response body is not valid JSON (nor a JSON fragment) as you have requested by your use of AFJSONResponseSerializer. A basic AFHTTPResponseSerializer can be used for responses that are not JSON.

    0 讨论(0)
  • 2020-12-29 04:37

    Hey guys this is what I found to be my issue: I was calling Alamofire via a function to Authenticate Users: I used the function "Login User" With the parameters that would be called from the "body"(email: String, password: String) That would be passed

    my errr was exactly:

    optional(alamofire.aferror.responseserializationfailed(alamofire.aferror.responseserializationfailurereason.jsonserializationfailed(error domain=nscocoaerrordomain code=3840 "invalid value around character 0." userinfo={nsdebugdescription=invalid value around character 0

    character 0 is the key here: meaning the the call for the "email" was not matching the parameters: See the code below

    func loginUser(email: String, password: String, completed: @escaping downloadComplete) { let lowerCasedEmail = email.lowercased()

        let header = [
            "Content-Type" : "application/json; charset=utf-8"
        ]
        let body: [String: Any] = [
            "email": lowerCasedEmail,
            "password": password
        ]
    
        Alamofire.request(LOGIN_USER, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
            if response.result.error == nil {
    
                if let data = response.result.value as? Dictionary<String, AnyObject> {
                    if let email = data["user"] as? String {
                        self.userEmail = email
                        print(self.userEmail)
                    }
                    if let token = data["token"] as? String {
                        self.token_Key = token
                        print(self.token_Key)
                    }
    

    "email" in function parameters must match the let "email" when parsing then it will work..I no longer got the error...And character 0 was the "email" in the "body" parameter for the Alamofire request:

    Hope this helps

    | improve this answer | |
    0 讨论(0)
  • 2020-12-29 05:00

    I am pretty sure that you have a valid response from server but your response is not a valid format in JSON, probably because you have carachters in front of first { . Please try this: Put the same URL address manually in your browser and you will see the culprit in the response. Hope it helped.

    0 讨论(0)
提交回复
热议问题