2017-01-30 84 views
0

我很努力地得到使用Alamofire和它们返回的json错误消息。 success/.failure方法。从Alamofire获取返回的错误消息

在使用此之前,我可以使用response.result.value并获取返回的错误消息,但现在我正在验证状态代码.validate(statusCode: 200..<300)

已经尝试了几件事情来接收错误,但它总是产生零或只是状态码。

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" 
} 
+0

你在使用print(response.result.error.debugDescription)的输出中得到了什么? –

+0

@AlexMcPherson'Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(404)' – Allreadyhome

+0

因为API产生403 – Allreadyhome

回答

1

采取.validate()出来。你会看到更详细的描述。