2011-06-26 25 views
0

「數據」可變的,我認爲它會有點像這樣:如何獲得的數據列於jQuery和Django的

筆者認爲:

from django.core import serializers 

後來....

data = serializers.serialize('json', MODEL.objects.filter(id=id), fields=('points')) 
return HttpResponse(data) 

在我的jQuery:

$.ajaxSetup({ 
    dataType: "json" 
}); 

$('#selector .selector_detail a').click(function() { 
    var call_to = $(this).attr('href'); 

    $.ajax({ 
     url: call_to, 
     type: "POST", 

     complete: function() { 
      console.log('Ajax Complete') 
     }, 

     success: function(data) { 
      points = data(fields.points) 
      console.log('Ajax Successful') 
      console.log(data); 
    }, 

     error: function(xhr) { 
      console.log('Whoops, something went wrong. XHR Response:' + JSON.stringify(xhr)); 
     }, 
    }); 

    return false; 
}); 

我想要點的價值,但我不知道如何把它拿出來。當我查看數據對象時,我可以在console.log中看到它。我錯過了什麼?

+0

「console.log」語句打印了什麼?我猜'data'或者*是'points'對象或者包含一個屬性'points'。 –

+0

它打印出來:'[{「pk」:3,「model」:「pointify.hero」,「fields」:{「points」:2758}}]' –

回答

1

如果數據是JSON對象和正確的標頭設置,您可以訪問它的使用點屬性:

data.points 

data[0].points //if points is an array 
//this is not correct 
data(fields.points); 

我不知道什麼是「數據」的確切結構做,但你可以得到你它來自你的console.log(數據);

編輯 - 如果數據有你在註釋中概述的結構,你可以接入點是這樣的:

alert(data[0].fields.points); 
+0

這是我從數據中得到的: ' [{「pk」:4,「model」:「pointify.hero」,「fields」:{「points」:3102}}]' 我很新,所以我不知道如何點擊fiels中的點數。它看起來像一個數組中的數組? –

+0

我爲您的數據結構編輯了我的答案 –

+0

真棒!非常感謝。快速提問[0]。這不表示數組中的數字嗎?我試圖找到位置[3](基於0計數的#4),但這似乎不起作用。 –

0

附加數據類型:「JSON」到您的通話阿賈克斯。

$.ajax({ 
    url: call_to, 
    dataType: 'json', 
    type: "POST", 

然後它的jut data.points在你的成功函數中,或者data.field.points。我無法從你的帖子中看出來。

相關問題