1

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';                        
    }
2
  • What is in the Django dict object? How does the JSON object for that look? Are you sure .fields is there? Commented Mar 25, 2014 at 7:39
  • @Bibhas I have made some updates please check Commented Mar 25, 2014 at 7:43

2 Answers 2

2

Your response.user_cards_held is an array object -

>> [{ "model" : "object.list", "pk" : 1, "fields" : { "name" : "...", ... } }]
// ^----- Array

So of course response.user_cards_held.fields would be undefined. Your actual object is at response.user_cards_held[0] So you can access the fields attribute like response.user_cards_held[0].fields.

Sign up to request clarification or add additional context in comments.

2 Comments

That means response.user_cards_held is a JSON string not a JSON object. You have to parse it as a JSON object first.
thanks a lot it worked fine after I parsed the other variables as well.
0
[{ "model" : "object.list", "pk" : 1, "fields" : { "name" : "José", ... } }]

// convert JSON string to JSON Object:<br>
var data = $.parseJSON(response);<br>
// acess name atribute<br>
**data[0].fields.name**;

2 Comments

While the code may answer the question, it is always best to include a description of why your solution works and any relevant references.
@TimHutchison I prefer the "shut up and show the code" method and the code that I put is commented out. The reference is my own app, since I did not find any reference when I looked. But I'll try to improve this, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.