表單提交我正在向PHP代碼發出AJAX請求,作爲響應,這是我得到的。JSON - 訪問值和循環
var data = {
"empty":{
"game_sais_no":"Season cannot contain empty value",
"game_sc_no":"Category cannot contain empty value",
"game_st_no2":"Visiting team connot contain empty value",
"game_room_no_2":"Visiting room cannot contain empty value",
"game_room_no_1":"Local chamber cannot contain empty value",
"game_date":"Game date should be specified",
"game_time":"Game time should be specified",
"game_time_start":"Game start time should be specified",
"game_time_close":"Game close time should be specified",
"game_place_no":"Arena \/ Lot should be specified",
"game_status":"Game status should be specified"
}
}
。我想訪問單個值。我試圖像這樣訪問它。
data.empty.game_sais_no it returns me the value of undefined.
。我想循環瀏覽json對象並將所有消息顯示回給用戶。我試着用
$.each(data, function(index, value)(){
//build the markup.
});
這是給我意想不到的結果。我哪裏錯了?
更新: 我不知道,但由於某種原因,它給了我奇怪的結果,讓我告訴你我到底在做什麼。
這裏是我的ajax調用php。
$('#gce_game_btn').on('click', function(){
var formData = $('#gce_game_form').serialize();
$.ajax({
type : 'POST',
url : 'accueil.php?m=ajax&game=1',
data : formData,
success : function(data) {
//
}
});
});
這裏是我試圖發送回陣列。
Array
(
[empty] => Array
(
[game_sais_no] => Season cannot contain empty value
[game_sc_no] => Category cannot contain empty value
[game_st_no2] => Visiting team connot contain empty value
[game_room_no_2] => Visiting room cannot contain empty value
[game_room_no_1] => Local chamber cannot contain empty value
[game_date] => Game date should be specified
[game_time] => Game time should be specified
[game_time_start] => Game start time should be specified
[game_time_close] => Game close time should be specified
[game_place_no] => Arena/Lot should be specified
[game_status] => Game status should be specified
)
)
我使用json_encode()
和呼應回來。這反過來又給了我這個字符串。
{
"empty":{
"game_sais_no":"Season cannot contain empty value",
"game_sc_no":"Category cannot contain empty value",
"game_st_no2":"Visiting team connot contain empty value",
"game_room_no_2":"Visiting room cannot contain empty value",
"game_room_no_1":"Local chamber cannot contain empty value",
"game_date":"Game date should be specified",
"game_time":"Game time should be specified",
"game_time_start":"Game start time should be specified",
"game_time_close":"Game close time should be specified",
"game_place_no":"Arena \/ Lot should be specified",
"game_status":"Game status should be specified"
}
}
試試這個:http://jsfiddle.net/juDFP/ – techfoobar
從來不知道我能做到這一點使用循環在JavaScript謝謝。如何訪問單個值。 –
'console.log(data.empty.game_sais_no);'對我很好用 –