フォームの送信時に、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"
}
}