小编tom*_*mpa的帖子

从错误中获取服务器响应消息

我的服务器(CakePHP)响应如下:

$this->response->statusCode('400');
$this->response->type('json');
$this->response->body(json_encode(array('message' => 'Bookmark already exists')));
Run Code Online (Sandbox Code Playgroud)

邮差输出看起来像你期望的那样:

{"message":"书签已存在"}

问题是我找不到从失败处理程序访问此消息的方法(Alamofire 3.1.3 + SwiftyJSON 2.3.2)

Alamofire.request(.POST...
.validate()
.responseJSON { response in

switch response.result {

case .Success(_):                           
// All good

case .Failure(let error):
// Status code 400                 
print(response.request)  // original URL request
print(response.response) // URL response
print(response.data)     // server data
print(response.result)
Run Code Online (Sandbox Code Playgroud)

我找不到一种方法将response.data转换为JSON,因为我只是得到nil,结果只返回FAILURE.

有没有办法从故障处理程序访问此服务器消息?

alamofire

9
推荐指数
3
解决办法
1万
查看次数

标签 统计

alamofire ×1