2013-02-28 18 views
0

我收到以下如何處理這個jQuery AJAX效應初探

[{"user_id":"1","user_invoice_add1":"Stark Towers","user_invoice_add2":"123 Reginald Street","user_invoice_city":"Newport","user_invoice_state":"New York","user_invoice_country":"US","user_invoice_zip":"321654"}] 

如何挑選出來自它的信息?

試圖data.user_id數據[0] .user_id收益爲未定義

jQuery的

$.post('post.php', qString, function (obj) { 
console.log(obj); 
} 
+0

顯示一次你的代碼,在使用數據 – PSR 2013-02-28 09:39:58

回答

1

你得先用這個字符串轉換成JSON對象一個jQuery函數var obj = $.parseJSON(data)。 這將返回一個JSON對象,您可以訪問諸如obj[0].user_id

0

使用這樣的:

myObject = jQuery.parseJSON(dataReceived); 

,然後你可以爲對象

0

嘗試這樣的性能訪問數據..

$.map(data.d, function (item) { 
    alert(item.user_id)     
}); 
0

jquery.ajax有一個設置叫做dataType這就是你從服務器得到的響應爲......

「json」:將響應評估爲JSON並返回JavaScript對象。在jQuery 1.4中,JSON數據以嚴格的方式進行解析;任何格式不正確的JSON都會被拒絕並引發解析錯誤。

$.ajax({ 
    .... 
    dataType:"json", 
    success:function(data){ 
     alert(data.user_id) //no need to parseJSON...dataType does it 
    }; 
}); 

使用POST

$.post('post.php', qString, function (obj) { 
    console.log(obj); 
}, "json"); 

,或者您可以使用$.parseJSON(data)的字符串作爲JSON對象轉換