1

我刚刚开始使用 AFNetworking,但我似乎无法弄清楚如何让以下工作。

我的 URL 指向一个 PHP 文件,该文件已打印出从数据库中检索到的 JSON 数据,但使用 AFNetworking 我得到“预期内容类型”错误。

我的代码如下,但 URL 不同。

NSURL *url = [NSURL URLWithString:@"http://www.example.com/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation 
JSONRequestOperationWithRequest:request 
                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                            NSLog(@"JSON: %@", [json valueForKeyPath:@"results"]);
                        } failure:nil];

[operation start];
4

2 回答 2

5

我想到了!:D

<?php header("Content-type: text/json"); ?>

在屏幕上打印任何内容之前将其放在页面顶部,AFNetworking 会将其识别为 JSON

于 2012-03-20T20:10:20.927 回答
2

@Ashley 感谢分享这个。我有同样的问题,这解决了它。不过应该是

header("Content-type: application/json"); 

而不是你说的

于 2013-04-10T08:36:22.670 回答