我刚刚开始使用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];
最佳答案
我想到了! :D
<?php header("Content-type: text/json"); ?>
在将任何内容打印到屏幕上之前,请将其放在页面顶部,AFNetworking会将其识别为JSON
关于ios - AFNetworking-iOS在PHP打印时获取JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791480/