I have a Ajax function which gets the Django object from a view.
I want to access all the attributes of the object in jQuery but I am not able to do so.
$.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);
},
});
View is like this:
from django.core import serializers
...
json = serializers.serialize('json', objectlists)
return HttpResponse(json, mimetype="application/json")
Object is like this :
[{ "model" : "object.list", "pk" : 1, "fields" : { "name" : "...", ... } }]
I have checked this question :Question Referenced This does not work. What am I doing wrong?
Edit:
To get the field properly I have made the following changes in the success function -
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';
}
.fieldsis there?