0

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

3 に答える 3

1

それは正常に動作します。デモを確認してください

$.each(data, function(index, value){
    console.log(index);
    $.each(value, function(index, value) {
       console.log(index, value);
    });
});
​
于 2012-09-25T06:18:31.107 に答える
1

まず、応答が配列で返されません。そうでした、それは次のように見えるはずです。とを参照して[ください]

"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"
    }]

次に、あなたは次のように読むでしょう

$.each(response.empty, function(index) {
           alert(response.empty[index].game_sais_no);

        });
于 2012-09-25T06:16:14.323 に答える
1

ブラウザのコンソール セクションにエラーが表示されますか?? json オブジェクトにアクセスしようとしている方法に問題はありません

これを試して

$.each(data.empty, function(i,value){
        console.log(value);
    }) ; 

フィドルをチェック

更新しました

ajax リクエストにdataType: 'json' 属性がないようです。データを文字列として解析することを指定しない場合

$.ajax({
        type    : 'POST',
        url     : 'accueil.php?m=ajax&game=1',
        data    : formData,
        dataType: 'json'
        success : function(data) {
            //
        }
    });
于 2012-09-25T06:34:32.173 に答える