我正在使用

NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:@"paths/" parameters:params];

AFJSONRequestOperation *jsonRequest = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request success:^(NSURLRequest *request,
NSHTTPURLResponse *response, id JSON) {
       // something to do here in case of success
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON
{
      // and something there in case of failure
}

一切正常,除了在一种情况下,当我收到带有 400状态代码的响应以及一个包含有关此错误的一些信息的JSON(有效,我已经检查过)时,如我在浏览器中看到的,
但是JSONRequestOperationWithRequest调用成功块,而JSON和response为零。
是什么原因造成的?

最佳答案

如果requestOperation完成后具有相关的错误,则调用失败。错误的原因包括:响应的Content-Type不正确,状态代码不可接受(默认为2XX范围,但是可以使用AFHTTPRequestOperation +addAcceptableStatusCodes:进行更改),或者处理下载的数据时出错。这是有保证的行为。

作为failure块的一部分,您可能会在400个响应中获得JSON对象,而在nil中,您可能会得到success JSON。这些都与AFNetworking的工作方式一致。

09-10 11:44
查看更多