ios - AFNetworking-iOS在PHP打印时获取JSON

标签 ios json content-type afnetworking

我刚刚开始使用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/

相关文章:

iphone - 适用于 iOS 的文本阅读器 API

ipad - 如何在 iPad 中使用 CTRunDelegate?

java - 如何从 Twitter API Java JSON 知道推文中的照片数量

用于 TCP 套接字的基于 C++ 流的 JSON 解析器

c# - WCF Post 请求内容类型 text/xml

ios - 在应用程序运行时启用/禁用核心数据的 iCloud 同步

ios - Xamarin iOS 中心 TableViewCell subview

javascript - ExtJS 加载嵌套 JSON

Ruby HTTP 发布请求 - 内容类型?

html - 我如何告诉 Apache 将没有扩展名的文件作为 text/html 提供?