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)
but it throws me "[Object Object]" from the alert prompt, any ideas, clues, help, suggestions, remendations?
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)
but it throws me "[Object Object]" from the alert prompt, any ideas, clues, help, suggestions, remendations?
Share Improve this question asked Nov 8, 2015 at 10:51 Juliver GalletoJuliver Galleto 9,04727 gold badges91 silver badges168 bronze badges 4-
Each of the
value
is anobject
, alert will make it a string, which impies a .toString on value, so you gets[Object Object]
. You can tryalert({})
which gives you the same result. If you just want to see that key-value pairs, you can make it ajson
again,alert(JSON.stringify(value))
, but if you just want to access it's values, usevalue.branch
... etc is ok. – fuyushimoya 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. – Juliver Galleto Commented Nov 8, 2015 at 10:57
-
alert
casts the objects to string, ThetoString
method on the GrandParentObject
class is called using prototype-chain, so it alerts[object Object]
. Useconsole.log
. – Tushar Commented Nov 8, 2015 at 10:58 - Here's my answer to a related issue on trying to alert objects. – Christofer Eliasson Commented Nov 8, 2015 at 11:01
4 Answers
Reset to default 3Do 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./WebReflection/circular-json which will allow to print even objects that references themselves (circular reference would fail to print big object).
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)
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 bees "[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);
});
}
}
If you want to alert that value than use alert(JSON.stringify(value)).