表单提交我正在向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);'对我很好用 –