【发布时间】:2015-11-28 02:54:03
【问题描述】:
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 验证器中验证为有效的静态 json 字符串。它确实包含 å ä ö 字符和一些 HTML。
为什么我有时会收到此错误?
【问题讨论】:
-
当我收到此错误时,我喜欢做的一件事是注释掉
responseJSON() { ... }块并替换为.responseString { _, _, s, _ in println(s) }。这使您可以查看返回的 json,以查找任何使其无法被responseJSON解析的奇怪文本 -
什么是响应状态码?
-
我得到一个状态码 200 并且我得到这个错误。啊哈。在我的情况下脑死亡:)。我实际上并没有从服务器返回 JSON。这样就解决了。
-
如果您使用 .POST 方法,这可能会起作用。
-
检查你的网址:)