2012-09-25 94 views
0

表单提交我正在向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" 
    } 
} 
+2

试试这个:http://jsfiddle.net/juDFP/ – techfoobar

+0

从来不知道我能做到这一点使用循环在JavaScript谢谢。如何访问单个值。 –

+0

'console.log(data.empty.game_sais_no);'对我很好用 –

回答

1

它工作正常。 Check the demo

$.each(data, function(index, value){ 
    console.log(index); 
    $.each(value, function(index, value) { 
     console.log(index, value); 
    }); 
}); 
​ 
+0

它不工作对我来说,在我的控制台打印出每一个变量。是否有任何错误,因为数据是响应接收'成功:函数(数据){//}' –

+0

@lbrahim您的代码不起作用并不意味着我的演示不起作用。你确定你的回调中的'data'是一个对象,如果它是字符串的,你需要将它解析为json,或者将你的ajax的'dataType'设置为'json'。 – xdazz

+0

我不使用dataType作为json,请更新我的问题。 –

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); 

     }); 
+0

我不想访问单个值,我只是想知道我做错了什么。 –

+0

我直接使用PHP中的'json_encode()'将数组转换为json对象。所以你认为我应该回应你说的那个? –

+0

我的意思是说,你没有得到阵列..一定有什么问题在服务器端 – Rab

1

您是否在浏览器的控制台部分看到任何错误? 没有什么错在你试图访问JSON对象

试试这个

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

检查FIDDLE

更新方式

你似乎缺少的dataType :'json'属性在您的ajax请求中。 如果您不指定它为par SES的数据作为一个字符串

$.ajax({ 
     type : 'POST', 
     url  : 'accueil.php?m=ajax&game=1', 
     data : formData, 
     dataType: 'json' 
     success : function(data) { 
      // 
     } 
    });