2012-09-25 92 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) { 
      // 
     } 
    });