【发布时间】:2017-06-15 12:26:02
【问题描述】:
我正在努力获取使用 Alamofire 及其 .success / .failure 方法返回的 json 错误消息。
在使用它之前,我可以使用 response.result.value 并获得返回的错误消息,但现在我正在验证状态代码 .validate(statusCode: 200..<300)。
已经尝试了几种方法来接收错误,但它总是产生 nil 或只是状态码。
Alamofire.request(url, method: .post, parameters: body, encoding: JSONEncoding.default)
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .success:
//Other stuff
case .failure(let error):
print(response.result.value) //Produces nil when there is an error
print(error.localizedDescription)
print(response.result.error.customMirror)
print(response.result.error.debugDescription)
print(response.result.error.unsafelyUnwrapped)
print(response.result.error?.localizedDescription)
}
}
如何获取错误 json?它正在被退回。
{
"status": "error",
"message": "Incorrect Password"
}
【问题讨论】:
-
使用 print(response.result.error.debugDescription) 在输出中得到什么?
-
@AlexMcPherson
Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(404) -
这有点奇怪,因为 API 会产生 403
-
好吧,找不到 404,所以这很奇怪。
-
事实上,如果我输入完整信息,它确实会返回 403!仍然没有拿起 json 虽然这令人沮丧。想用这个方法保持干净。