2

I have this on success AJAX function

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            alert(value);
        });
    }   
}

and this the JSON response from the console (refer to the image below)

JSON response from the console

but it throws me "[Object Object]" from the alert prompt, any ideas, clues, help, suggestions, recommendations?

4
  • Each of the value is an object, alert will make it a string, which impies a .toString on value, so you gets [Object Object]. You can try alert({}) which gives you the same result. If you just want to see that key-value pairs, you can make it a json again, alert(JSON.stringify(value)), but if you just want to access it's values, use value.branch... etc is ok. Commented Nov 8, 2015 at 10:54
  • so any ideas how to render it like a string in the alert prompt? like if im going to display the branch, i get each branch. Commented Nov 8, 2015 at 10:57
  • alert casts the objects to string, The toString method on the GrandParent Object class is called using prototype-chain, so it alerts [object Object]. Use console.log. Commented Nov 8, 2015 at 10:58
  • Here's my answer to a related issue on trying to alert objects. Commented Nov 8, 2015 at 11:01

4 Answers 4

3

Do not use alert but console.log instead. You will be able to look into all the objects that way and avoid getting spammed.

Also, if you need to look into a deep object, you can use something like https://github.com/WebReflection/circular-json which will allow to print even objects that references themselves (circular reference would fail to print big object).

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

2 Comments

so any ideas how to render it like a string in the alert prompt? like if im going to display the branch, i get each branch.
As @kishan wrote, you can use alert(JSON.stringify(value)), but this would fail if the object has circular reference, but just give it a try, it's totally fine for simple objects.
3

alert uses the object's toString method, which will return this [Object Object] thing. If you want to print an object nicely, you can use JSON.stringify(yourObject)

Comments

2

In you current code, the value is an object, however, alert can only display string, so it'll use .toString to convert your value to a string, which then becomes "[Object Object]".

To display the value as key-value pairs use JSON.stringify(value) to make it a json again:

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            alert(JSON.stringify(value));
        });
    }   
}

if you just want to access the attributes of the value, use their key should work:

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            // This will alert each items' `bundle` value.
            // It's enough in your case, but you may have to check if the target attribute you want to alert is also an object.
            alert(value.bundle);
        });
    }   
}

Comments

1

If you want to alert that value than use alert(JSON.stringify(value)).

Comments

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.