请求失败:不可接受的内容类型:使用AFNetworking 2.0的text / html [英] Request failed: unacceptable content-type: text/html using AFNetworking 2.0
问题描述
我正在尝试AFNetworking的新版本2.0,我收到上面的错误。知道为什么会这样吗?这是我的代码:
I'm trying out the new version 2.0 of AFNetworking and I'm getting the error above. Any idea why this is happening? Here's my code:
NSURL *URL = [NSURL URLWithString:kJSONlink];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];
我正在使用Xcode 5.0。
I'm using Xcode 5.0.
此外,这是错误消息:
Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0xda2e670 {NSErrorFailingURLKey=kJSONlink, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xda35180> { URL: kJSONlink } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 2898;
"Content-Type" = "text/html";
Date = "Tue, 01 Oct 2013 10:59:45 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = Apache;
Vary = "Accept-Encoding";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
我只是使用kJSONlink隐藏了JSON。这应该返回一个JSON。
I just hid the JSON using kJSONlink. This should return a JSON.
推荐答案
这意味着你的服务器正在发送text / html
而不是已经支持的类型。
我的解决方案是将text / html
添加到 acceptedContentTypes
中设置 AFURLResponseSerialization
类。只需搜索acceptableContentTypes并手动将 @text / html
添加到集合中。
This means that your server is sending "text/html"
instead of the already supported types.
My solution was to add "text/html"
to acceptableContentTypes
set in AFURLResponseSerialization
class. Just search for "acceptableContentTypes" and add @"text/html"
to the set manually.
当然,理想的解决方案是更改从服务器发送的类型,但为此您必须与服务器团队交谈。
Of course, the ideal solution is to change the type sent from the server, but for that you will have to talk with the server team.
这篇关于请求失败:不可接受的内容类型:使用AFNetworking 2.0的text / html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!