Alamofire.request(.GET, "url").authenticate(user: "", password: "").responseJSON() {
(request, response, json, error) in
println(error)
println(json)
}
这是我使用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.})
我读过一些资料,说这可能是由于无效的JSON导致的。但响应是一个包含校验过为有效的JSON字符串。该字符串包含了一些å ä ö字符和一些HTML标签。
为什么有时候会出现这个错误?
responseJSON() { ... }
块,并替换为.responseString { _, _, s, _ in println(s) }
。这样可以让你看到返回的 JSON,以寻找任何不符合responseJSON
解析条件的文本。请注意,此操作不会改变原意。 - ad121