Alamofire invalid value around character 0

前端 未结 11 1948
忘了有多久
忘了有多久 2020-11-29 02:26
Alamofire.request(.GET, \"url\").authenticate(user: \"\", password: \"\").responseJSON() {
    (request, response, json, error) in
    println(error)
    println(jso         


        
相关标签:
11条回答
  • 2020-11-29 02:59

    Maybe it is too late but I solved this problem in another way not mentioned here:

    When you use .responseJSON(), you must set the response header with content-type = application/json, if not, it'll crash even if your body is a valid JSON. So, maybe your response header are empty or using another content-type.

    Make sure your response header is set with content-type = application/json to .responseJSON() in Alamofire work properly.

    0 讨论(0)
  • 2020-11-29 03:00

    I solved using this as header:

    let header = ["Content-Type": "application/json", "accept": "application/json"]

    0 讨论(0)
  • 2020-11-29 03:02

    I got the same error. But i found the solution for it.

    NOTE 1: "It is not Alarmofire error", it's bcouse of server error.

    NOTE 2: You don't need to change "responseJSON" to "responseString".

    public func fetchDataFromServerUsingXWWWFormUrlencoded(parameter:NSDictionary, completionHandler: @escaping (_ result:NSDictionary) -> Void) -> Void {
    
            let headers = ["Content-Type": "application/x-www-form-urlencoded"]
            let completeURL = "http://the_complete_url_here"
            Alamofire.request(completeURL, method: .post, parameters: (parameter as! Parameters), encoding: URLEncoding.default, headers: headers).responseJSON { response in
    
                if let JSON = response.result.value {
                    print("JSON: \(JSON)") // your JSONResponse result
                    completionHandler(JSON as! NSDictionary)
                }
                else {
                    print(response.result.error!)
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-29 03:03

    I also faced same issue. I tried responseString instead of responseJSON and it worked. I guess this is a bug in Alamofire with using it with django.

    0 讨论(0)
  • 2020-11-29 03:07

    May this Help YOu

    Alamofire.request(.GET, "YOUR_URL")
         .validate()
         .responseString { response in
             print("Success: \(response.result.isSuccess)")
             print("Response String: \(response.result.value)")
         }
    
    0 讨论(0)
  • 2020-11-29 03:13

    I got same error while uploading image in multipart form in Alamofire as i was using

    multipartFormData.appendBodyPart(data: image1Data, name: "file")
    

    i fixed by replacing by

    multipartFormData.appendBodyPart(data: image1Data, name: "file", fileName: "myImage.png", mimeType: "image/png")
    

    Hope this help someone.

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