Alamofire字符0周围的值无效

Lor*_*ion 87 ios swift alamofire

Alamofire.request(.GET, "url").authenticate(user: "", password: "").responseJSON() {
    (request, response, json, error) in
    println(error)
    println(json)

}
Run Code Online (Sandbox Code Playgroud)

这是我对Alamofire的要求,因为某个时候有一定的要求,但有时我得到:

Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x78e74b80 {NSDebugDescription=Invalid value around character 0.})
Run Code Online (Sandbox Code Playgroud)

我已经读过这可能是由于JSON无效,但是响应是一个静态json字符串,我在JSON验证器中验证为有效.它包含åäö字符和一些HTML.

为什么我有时会收到此错误?

小智 126

我也面临同样的问题.我尝试了responseString而不是responseJSON工作.我想这是Alamofire使用它的一个错误django.

  • 节省了我的一天:) (4认同)
  • 感谢您指出了这一点.我使用的是responseJSON,但服务器的实际响应是XML格式的!救了我头疼:) (3认同)

Sae*_*eed 11

就我而言,我的服务器 URL 不正确。检查您的服务器 URL !!


Avi*_*are 9

我在Alamofire中使用多部分形式上传图像时遇到了同样的错误

multipartFormData.appendBodyPart(data: image1Data, name: "file")
Run Code Online (Sandbox Code Playgroud)

我通过替换来修复

multipartFormData.appendBodyPart(data: image1Data, name: "file", fileName: "myImage.png", mimeType: "image/png")
Run Code Online (Sandbox Code Playgroud)

希望这能有所帮助.


cam*_*eau 6

我遇到了同样的问题,实际上由于未设置内容类型,最终导致了服务器问题。

新增中

.validate(contentType: ["application/json"])
Run Code Online (Sandbox Code Playgroud)

到请求链为我解决了

Alamofire.request(.GET, "url")
        .validate(contentType: ["application/json"])
        .authenticate(user: "", password: "")
        .responseJSON() { response in
            switch response.result {
            case .Success:
                print("It worked!")
                print(response.result.value)
            case .Failure(let error):
                print(error)
            }
        }
Run Code Online (Sandbox Code Playgroud)


Kru*_*tel 6

愿这个帮助你

Alamofire.request(.GET, "YOUR_URL")
     .validate()
     .responseString { response in
         print("Success: \(response.result.isSuccess)")
         print("Response String: \(response.result.value)")
     }
Run Code Online (Sandbox Code Playgroud)