valor indefinido de javascript

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]

preguntado el 30 de enero de 12 a las 19:01

what calls load, what are you expecting data to be, what is data.shipments, Cuál es tu pregunta? -

Hi Hamish, load is for ajax get request, data type returned in application/json and I also posted the data snippet. -

well, it looks like everything is working. what is your question? -

Can you give us more information about what library you are using to make the request? The code calling the function and what you are passing into the load function is going to matter a lot. Try printing data itself and see what you get? -

yet to finalize on jQuery or standard or Dojo. Right now I am playing with Dojo. -

3 Respuestas

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.

Respondido el 30 de enero de 12 a las 23:01

It looks like the response is working fine.

Trata 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.

Respondido el 30 de enero de 12 a las 23:01

Does the response contain a "responseText" ¿propiedad?

{"shipments": [...], "responseText": ..., ...}

If not, then it makes sense for data.responseText para ser undefined as it wasn't defined.

Si estás buscando el 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?).

En cuanto a los "[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);

Respondido el 30 de enero de 12 a las 23:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.