我目前使用下面的脚本打印JSON文件保存为JSON文件用PHP打印JSON,然后
<?PHP
include ('connect.php');
$get_student = mysql_query("SELECT * FROM student ORDER BY name asc");
$anArray = json_decode ($data);
while ($row = mysql_fetch_assoc($get_student)) {
$anArray[] = $row;
}
header("Content-type: application/json");
echo json_encode ($anArray, JSON_PRETTY_PRINT)
?>
我然后使用AFNETWORKING获取在Xcode JSON文件并将其保存到文档目录
url = [NSURL URLWithString:@"url/Json3.php"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFHTTPRequestOperation *downloadOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"database"];
downloadOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[downloadOperation start];
我再尝试读取下载的文档具有下列
NSURL *JsonUrl = [NSURL fileURLWithPath:path];
NSURLRequest *JSONrequest = [[NSURLRequest alloc] initWithURL:JsonUrl];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:JSONrequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
anArray = JSON;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
一个JSON文件10
然而AFNetworking给我在日志以下错误线
请求失败,错误:错误域= AFNetworkingErrorDomain代码= -1016“预期内容类型{( “文本/ JSON”, “应用/ JSON”, “文本/ JavaScript的” )},得到纯文本/”的UserInfo = 0xa49f990 {NSLocalizedRecoverySuggestion = [
,然后示出了日志
QUESTION
0在文本文档内容如何进行PHP JSON打印,打印Application/Json文档而不是文本文档?
感谢
我猜在发送'header('Content-Type:application/json');'在PHP中发生错误。 – hek2mgl 2013-04-08 13:30:17
检查_“头已发送”_警告。 – Halcyon 2013-04-08 13:32:41
@FritsvanCampen我已经检查过头文件,并且可以确认它们尚未发送 – Tom 2013-04-10 08:29:55