2010-12-08 59 views
1

任何人都可以幫助我。我正在使用以下代碼在jQuery Mobile中調用Web服務。但是我收到錯誤「未定義」。請指出我犯了錯誤的地方。提前致謝。JQueryMobile - AJAX - JSON解析

編碼:

$.ajax({ 
type: 'POST', 
url: "http://jquery.sample.com/nodes.json", 
data: ({search_keys :theName}), 
dataType: 'json', 
timeout: 5000, 
success: function(msg) 
{ 
    console.log(msg);  //here, I can see the result in browser. 
    alert(msg.message); //Undefined Error 
}, 
error: function(xhr, status, errorThrown) 
{ 
alert(status + errorThrown); 
} 
});  

JSON輸出
[ { 「類型」: 「業務配置文件」, 「標題」: 「萊克維爾餐館」, 「用戶」:」 canwest「, 」date「:」1280144992「, 」node「:{ 」nid「:」67916「, 」type「:」business_profiles「, 」language 「:」」, 「UID」: 「1」, 「狀態」: 「1」, 「創造」: 「1278994293」 }} ]

回答

2

你得到一個數組回來,不一個基本對象 - 而且當時也沒有message財產,我可以看到,所以它應該是:

alert(msg[0].title); 

或者,通過他們所有的循環 - 例如:

$.each(msg, function(i, profile) { 
    alert(profile.type); 
    alert(profile.node.nid); 
}); 
+0

感謝尼克Craver。 – Finder 2010-12-08 09:22:45