Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法从Alamofire 3.3.0获取服务器错误消息_Ios_Xcode_Swift_Alamofire - Fatal编程技术网

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

Ios 无法从Alamofire 3.3.0获取服务器错误消息,ios,xcode,swift,alamofire,Ios,Xcode,Swift,Alamofire,这是我第一次使用Alamofire,这让我非常沮丧 我使用以下代码在后端API上调用注册API Alamofire.request(.POST, "\(self.authBaseURL)/signup", parameters: params, headers: headers, encoding: .JSON) .validate(statusCode: 200..<300) .validate(contentType: ["applica

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

我使用以下代码在后端API上调用注册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)
                }

        }
Alamofire.request(.POST,“\(self.authBaseURL)/signup”,参数:params,headers:headers,编码:.JSON)

.validate(statusCode:200..我通过放弃状态验证并手动检查statusCode,使其完全按照我想要的方式工作

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)
                }


        }
为swift 3更新:

用于以下代码行:-

    Alamofire.request(escapedString!, method: .get, encoding: JSONEncoding.default)
        .validate(contentType: ["application/json"])
        .responseJSON { response in
            if response.response?.statusCode == 200 {
                print("Success with JSON: \(String(describing: response.result.value))")

            }
            else {
                let error = (response.result.value  as? [[String : AnyObject]])
                print(error as Any)
            }
    } 

这是使用Alamofire 4和Swift 3获取错误域、代码和用户信息的方法。用户信息包含错误字符串

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):
                    let errorCode = error._code
                    let errorDomain = error._domain
                    let userInfo = error._userInfo
                    print("Request failed with error: \(error), code: \(errorCode), domain: \(errorDomain)")
                    failure(error)
                }

        }
Alamofire.request(.POST,“\(self.authBaseURL)/signup”,参数:params,headers:headers,编码:.JSON)

.validate(statusCode:200..So,而不是
.responseJSON
尝试
.responseString
并执行
打印(响应)
并查看您是否得到了不应该得到的信息。稍后,当您发现问题时,请切换回
。responseJSON
已经这样做了,但是我得到的错误仍然是一样的。服务器正在抛出正确的错误消息(我用AngularJS创建了另一个客户端)但我还是没能把它弄进去ios@JosephWahba你能解决你原来的问题吗?也就是说,获取错误域、代码、用户信息数据?我正在尝试做同样的事情。如果你能提供帮助,你能在上查看我的问题吗?我无法获取404状态代码的服务器错误日志,数据字段始终为零,并且获取错误信息is响应[结果]:失败:错误域=NSURErrorDomain代码=-1015“无法解码原始数据”这不仅不起作用,而且如果result.value为零,它将崩溃,这通常适用于AlamoFire结果。@Randy Hill,我更新了上面的代码……现在它工作正常了……!请再次检查,并让我知道您是否有任何问题?我删除了我的否决票,因为它现在不会崩溃,而且它是使用.responseJSON的合理示例。但是使用的问题验证(状态代码:200..@Randy Hill,,,,,我将尝试为您的问题提供最好的解决方案???感谢您选择上述解决方案。。!