標題 json 和返回的響應

通過新增內容型別為 JSON 的標頭:

<?php
 $result = array('menu1' => 'home', 'menu2' => 'code php', 'menu3' => 'about');

//return the json response :
header('Content-Type: application/json');  // <-- header declaration
echo json_encode($result, true);    // <--- encode
exit();

標題在那裡,因此你的應用程式可以檢測返回的資料以及應如何處理它。
請注意: 內容標題只是有關返回資料型別的資訊。

如果你使用的是 UTF-8,則可以使用:

header("Content-Type: application/json;charset=utf-8");

示例 jQuery:

$.ajax({
        url:'url_your_page_php_that_return_json'        
    }).done(function(data){
        console.table('json ',data);
        console.log('Menu1 : ', data.menu1);
    });