AFNetworking 2.0在故障块中从代码400获取JSON [英] AFNetworking 2.0 Get JSON from code 400 in failure block

查看:163
本文介绍了AFNetworking 2.0在故障块中从代码400获取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AFHTTPRequestOperationManager 获取 POST 请求。现在我故意输入不正确的信息来处理 400 错误代码。现在,Web服务实际上返回一个 JSON ,其中包含一条消息,向用户解释他们做错了什么。我非常希望得到这个 JSON 来在 UIAlertView 中显示消息。但是,失败块:

I'm using AFHTTPRequestOperationManager for a POST request. Now I'm deliberately entering incorrect information to handle a 400 error code. Now, the web service actually returns a JSON with a message explaining to the user what they've done wrong. I would very much like to get this JSON to display the message in a UIAlertView. However, the failure block of:

[operationManager POST:ServerURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: Status Code: %d", operation.response.statusCode);
}
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Failed: Status Code: %d", operation.response.statusCode);
 }];

不传递响应对象,如成功块中的那个。那么有谁知道如何访问由Web服务返回的400错误的JSON? NSError *错误只是给我请求失败:错误请求(400)而不是返回JSON。

doesn't pass down a responseObject like the one in the success block. So does anyone know how I can access the JSON returned by the Web Service with the 400 error? The NSError *error simply gives me Request failed: bad request (400) and not the JSON returned.

任何帮助将不胜感激,迈克

Any help would be appreciated,
Mike

推荐答案

看着代码为 - responseObject ,似乎HTTP错误阻止了它的填充。您可以直接获取 responseData 并自行解析,但我会说这是一个错误或一个好的增强请求。 (看起来像 - responseObject 可能应该检查 self.responseSerializationError ,而不是 self.error ,在决定是否应该尝试构建响应对象时。)

Looking at the code for - responseObject, it appears that an HTTP error prevents it from being populated. You can grab the responseData directly and parse it yourself, but I would say this is either a bug or a good enhancement request. (It looks like - responseObject probably should be checking self.responseSerializationError, not self.error, when deciding if it should try to build a response object.)

这篇关于AFNetworking 2.0在故障块中从代码400获取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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