ios - 请求失败 : unacceptable content-type: text/html using AFNetworking 2. 0

标签 ios afnetworking afnetworking-2

我正在试用 AFNetworking 的新版本 2.0,但出现上述错误。知道为什么会这样吗?这是我的代码:

    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。

此外,这是错误消息:

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。

最佳答案

这意味着您的服务器正在发送 "text/html" 而不是已经支持的类型。 我的解决方案是将 "text/html" 添加到 AFURLResponseSerialization 类中设置的 acceptableContentTypes。只需搜索“acceptableContentTypes”并手动将 @"text/html" 添加到集合中。

当然,理想的解决方案是更改从服务器发送的类型,但为此您必须与服务器团队交谈。

关于ios - 请求失败 : unacceptable content-type: text/html using AFNetworking 2. 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19114623/

相关文章:

Objective-C iOS NSURLConnection statusCode 400 没有正文返回

ios - AFNetworking 发送带有请求的 header

ios - 仅在尚未下载的情况下,以较低优先级在后台以较低的优先级在iOS 7中下载一批文件

ios - 异步环境中的同步调用

ios - 变暗 UIImageView IOS

ios - 响应 child 和 parent 的触摸事件

ios - 如何在 swift 3 的 UITableView 中创建和使用变量

objective-c - AFNetworking 证书错误

ios - Phasset + AfNetworking 上传多个视频

php - 如何通过 AFNetworking API 调用使用 gzip 解压缩?