无法从Alamofire 3.3.0获取服务器错误消息

编程入门 行业动态 更新时间:2024-10-19 22:18:44
本文介绍了无法从Alamofire 3.3.0获取服务器错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我第一次使用 Alamofire ,这让我非常沮丧。

It is my first time for me to use Alamofire, and it got me really frustrated.

I使用以下代码在后端API上调用注册API

I'm using the following code to call a signup API on the backend API

Alamofire.request(.POST, "\(self.authBaseURL)/signup", parameters: params, headers: headers, encoding: .JSON) .validate(statusCode: 200..<300) .validate(contentType: ["application/json"]) .responseJSON { response in switch response.result { case .Success(let JSON): print("Success with JSON: \(JSON)") success(updatedUser) case .Failure(let error): print("Request failed with error: \(error)") failure(error) } }

问题是错误对象我进入 .Failur e 函数不包含服务器端消息。 我试图访问其余的对象(请求,响应,数据,结果)我无法在任何地方找到我的错误消息

The problem is that the error object I'm getting in the .Failure function doesn't contain the server side message. I have tried to access the rest of the objects (request, response, data, result) I could not find my error message anywhere

我总是无论服务器消息说什么,都会收到以下错误。 请求失败并显示错误:

I'm always getting the following error, no matter what the server message has to say. Request failed with error:

FAILURE:Error Domain = com.alamofire.error Code = -6003响应状态代码是不可接受的:400 UserInfo = {NSLocalizedFailureReason =响应状态代码是不可接受:400}

FAILURE: Error Domain=com.alamofire.error Code=-6003 "Response status code was unacceptable: 400" UserInfo={NSLocalizedFailureReason=Response status code was unacceptable: 400}

我有什么问题吗?

Swift 2.2,AlamoFire 3.3.0,Xcode 7.3

Swift 2.2, AlamoFire 3.3.0, Xcode 7.3

推荐答案

我设法让它按照我想要的方式工作,方法是删除状态验证并手动检查statusCode

I managed to get it to work exactly the way I want is by dropping the status validation and check for the statusCode manually

Alamofire.request(.POST, "\(self.authBaseURL)/signup", parameters: params, headers: headers, encoding: .JSON) .validate(contentType: ["application/json"]) .responseJSON { response in if response.response?.statusCode == 200 { print("Success with JSON: \(response.result.value)") success(updatedUser) } else { let error = response.result.value as! NSDictionary let errorMessage = error.objectForKey("message") as! String print(errorMessage) failure(errorMessage) } }

更多推荐

无法从Alamofire 3.3.0获取服务器错误消息

本文发布于:2023-11-26 18:18:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   消息   服务器   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!