3

我正在使用 AFNetworking 处理从我的移动应用程序到我的 Rails 服务器的重置密码请求。

api正在返回:head:ok(结果为200)

但是,这会导致 AFNetworking 在我发出getPath请求时运行失败块。

我可以做两件事来运行成功块:

  1. 有 api 返回head :no_content(导致 204)
  2. 不要将我的 Accept 标头设置为 `application/json'

当状态码为 200 且 Accept 标头为application/json.

我没有完全控制 api,所以是否有可能有一个没有内容的 200 仍然触发我的成功块,或者 204 应该用于这种确切的情况,它成功但不会返回任何内容服务器?

谢谢!

4

1 回答 1

2

AFHTTPRequestOperation您可以自己使用和处理响应代码,而不是使用 AFNetworking 的成功和完成块。例如:

// Setup HTTP client
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://website.com"]];

// Create the request
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"authenticate" parameters:params];

// Create an HTTP operation with your request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Setup the completion block
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    switch (operation.response.statusCode) {
        case 200:
            // Do stuff
            break;
        default:
            break;
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    switch (operation.response.statusCode) {
        case 400:
            // Do stuff
            break;
        default:
            break;
    }
}];

// Begin your request
[operation start];
于 2012-11-12T20:11:21.790 回答