无法从Alamofire 3.3.0获取服务器错误消息 [英] Could not get the server error message from Alamofire 3.3.0

查看:619
本文介绍了无法从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获取服务器错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆