보기에서 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 . 내가 뭘 잘못하고 있니?
편집 :
이 제대로 나는 성공 함수에서 다음과 같이 변경 만든 분야 얻으려면 -
>> [{ "model" : "object.list", "pk" : 1, "fields" : { "name" : "...", ... } }]
// ^----- Array
물론 그래서 -
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';
}
장고 dict 개체에는 무엇이 있습니까? JSON 개체는 어떻게 보이나요? '.fields'가 확실합니까? –
@Bibhas 나는 약간의 업데이 트를 확인했다. –