我目前使用下面的腳本打印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