我正在使用 AFNetworking 来处理从我的移动应用程序到我的 Rails 服务器的重设密码请求。
API 正在返回: head :ok(结果为 200)
但是,当我发出 getPath
请求时,这会导致 AFNetworking 运行失败 block 。
我可以做两件事来运行成功 block :
- 让 api 返回
head :no_content
(结果为 204) - 不要将我的 Accept header 设置为“application/json”
当状态代码为 200 且 Accept header 为 application/json
时,AFNetworking 似乎至少需要一个空数组。
我没有完全控制 api,所以是否有可能没有内容的 200 仍然触发我的成功 block ,或者 204 应该用于这种确切的情况,它成功但什么都不会从服务器返回?
谢谢!
最佳答案
您可以使用 AFHTTPRequestOperation
并自行处理响应代码,而不是使用 AFNetworking 的成功和完成 block 。例如:
// 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];
关于iphone - AFNetworking:返回 200 且无内容时运行失败 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044880/