我有一個從視圖中獲取Django對象的Ajax函數。如何在jQuery中訪問Django對象
我想訪問jQuery中的對象的所有屬性,但我不能這樣做。
$.ajax(
{
url: domain+'/game/android/',
type:"POST",
success:function(response){
response = jQuery.parseJSON(response);
localStorage['userCard'] = response.user_cards_held;
localStorage['compCard'] = response.comp_cards_held;
localStorage['game'] = response.game;
alert(response.user_cards_held);// **this shows all the fields. **
alert(response.user_cards_held.fields);//This does not work. Gives the value as undefined
window.location = 'file:///android_asset/www/game.html';
},
error:function(xhr, status, error){
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
},
});
觀是這樣的:
from django.core import serializers
...
json = serializers.serialize('json', objectlists)
return HttpResponse(json, mimetype="application/json")
對象是這樣的:
[{ "model" : "object.list", "pk" : 1, "fields" : { "name" : "...", ... } }]
我已經檢查了這個問題:Question Referenced 這是行不通的。 我在做什麼錯?
編輯:
獲取字段妥善我已經取得了成功的功能如下變化 -
success:function(response){
response = jQuery.parseJSON(response);
localStorage['userCard'] =jQuery.parseJSON(response.user_cards_held);
localStorage['compCard'] = jQuery.parseJSON(response.comp_cards_held);
localStorage['game'] = jQuery.parseJSON(response.game);
alert(jQuery.parseJSON((response.user_cards_held));// **this shows all the fields. **
alert(jQuery.parseJSON(response.user_cards_held.fields));//This does not work. Gives the value as undefined
window.location = 'file:///android_asset/www/game.html';
}
什麼是Django dict對象? JSON對象的外觀如何?你確定有'.fields'嗎? –
@Bibhas我已經做了一些更新,請檢查 –