I am making an Ajax that returns JSON. I can see the returned JSON in Firebug and validated it against JSLint but I am not able to extract the content. Heres what I am seeing:
Data: {"shipments":[{"companyName":"GLOBAL SOL INC.", so on...........}]}
load: function(data){
console.log("responseText " + data.responseText);
**Above results in: responseText undefined**
var myVar = data.shipments;
console.log("myVar " + myVar);
Above results in myVar [Object object]
In your example, myVar refers to an array that contains objects. To get the companyName value, use data.shipments[0].companyName
or myVar[0].companyName
.
It looks like the response is working fine.
Try console.log(data)
or console.log(myVar)
. If you invoke console without the Strings then it will display the contents of the object (at least in Chrome).
Then you'll be able to tell the elements of the object that you need to ask for. It's likely it's a JSON formatting issue, but it's difficult to tell from your question.
Does the response contain a "responseText"
property?
{"shipments": [...], "responseText": ..., ...}
If not, then it makes sense for data.responseText
to be undefined
as it wasn't defined.
If you're looking for the responseText
from the XMLHttpRequest
object, it will probably be a different argument to the load
function. Which argument that is depends on the script that calls the function (a particular library or home-brewed?).
As for "[Object object]"
, this is the default toString
result for an Object
. If you want the JSON representation again, use JSON.stringify(...)
:
console.log("myVar " + JSON.stringify(myVar));
Or, most browser consoles offer the ability to browse an object. To use this, just pass it as a separate argument:
console.log("myVar", myVar);