请求失败:不可接受的内容类型:使用 AFNetworking 2.0 的文本/html [英] Request failed: unacceptable content-type: text/html using AFNetworking 2.0

查看:32
本文介绍了请求失败:不可接受的内容类型:使用 AFNetworking 2.0 的文本/html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用 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.

另外,这里是错误信息:

Also, here's the error message:

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" 添加到 acceptableContentTypes 设置在 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 的文本/html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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