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

首先,您的响应未在数组中返回。它是,它应该看起来像。看到[]

"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

它工作正常。检查演示

$.each(data, function(index, value){
    console.log(index);
    $.each(value, function(index, value) {
       console.log(index, value);
    });
});
​
于 2012-09-25T06:18:31.107 回答
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 回答