1
我有一個test.php
這樣的數組。爲什麼我不能訪問爲json
Array(
[0] => Array
(
[id] => 1
[name] => nikhil
[password] => 81dc9bdb52d04dc20036dbd8313ed055
)
[1] => Array
(
[id] => 2
[name] => akhil
[password] => 81dc9bdb52d04dc20036dbd8313ed055
)
)
我把它轉換成JSON和回聲它
$jsonformat = json_encode($array);
echo $jsonformat;
然後我得到一個JSON字符串,這樣就test.php
[{
"id": 1,
"name": "nikhil",
"password": "81dc9bdb52d04dc20036dbd8313ed055"
},{
"id": 2,
"name": "akhil",
"password": "81dc9bdb52d04dc20036dbd8313ed055"
}]
現在,我怎麼能訪問作爲JSON使用此字符串AJAX呼叫。我想只訪問這個JSON對象的第一行的名稱。我試過這個:
$.ajax({
type: 'post',
url: '/test.php',
datatype: 'JSON',
success: function(result) {
alert(result[0].name);
}
});
但是這是行不通的。有什麼建議麼?
你到達'success'功能(是你的JSON有效...)?如果是這樣,'result'包含什麼,'console.log(result);'? – jeroen
'成功:功能(結果){ var data = $ .parseJSON(result); alert(data.name);} //或者嘗試一次alert(data [0] .name) });' –
順便說一下,您可能只需要將'datatype'更改爲'dataType'來確保jQuery自動分析返回的json。 – jeroen