在表单提交上,我正在向 PHP 代码发出 AJAX 请求,作为回应,这就是我得到的。
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"
}
}
1 . 我想访问单个值。我试着像这样访问它。
data.empty.game_sais_no it returns me the value of undefined.
2 . 我想遍历 json 对象并将所有消息显示给用户。我尝试使用
$.each(data, function(index, value)(){
//build the markup.
});
这给了我意想不到的结果。我哪里错了?
更新: 我不确定,但由于某种原因,它给了我奇怪的结果,让我告诉你我到底在做什么。
这是我对 php 的 ajax 调用。
$('#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"
}
}