Visualización de la respuesta ColdFusion JSON con jQuery

I'm have trouble grabbing the response JSON data that's returned via AJAX call. Here is what I’m getting back in firebug.

{"COLUMNS":["ID","NAME","REGION","EMAIL"],"DATA":[["1234","John Doe","West","johndoe@mydomain.com"]]}

Aquí está mi guión:

$.ajax({  
        url: "action.cfc?method=getEmployees&returnformat=json&queryFormat=column",         type: "POST",  
           success: function(response){
                      console.log(response);// is the data above
                      console.log(response.DATA.NAME[1]); // this doesn’t work
                      console.log(response.DATA['NAME']);//and this doesn’t work
  }
});

I'm getting this error in fireBug "response.DATA is undefined" when trying to console log the name. What am I missing? Thank in advance for your help!

preguntado el 10 de marzo de 12 a las 00:03

3 Respuestas

response.DATA is an array of arrays so you need to index each level.

   console.log(response.DATA[0][1]); 

EDIT: if you want to use key/value pairings you would need to create a struct in CF instead of array

You should also set dataType:'json' in $.ajax

respondido 10 mar '12, 00:03

I've found it useful to return structs rather than queries from CF web services if I'm going to use the results in jQuery. - David Faber

Maybe you need to parse it from a string into an object:

success: function(response){
    console.log(response);// is the data above
    response = JSON.parse(response)
    console.log(response);// is the data above
}

respondido 10 mar '12, 00:03

Thanks Billy, just ran into another problem and parsing the response into an object solved it. - user752746

They say IE8 is the new IE7, and IE7 was the new IE6! There are many problems with them all, but it seems like the later versions of IE are finally becoming a bit more DOM compliant. - billy luna

Looping through the response recieved, using $.each() jquery function, will be able to access the data returned from CF.
For the above response:
Example,
$.each(response,function(mainKey,mainValue){
    console.log(mainKey); //displays 'COLUMNS' & 'DATA'
    console.log(mainValue);// displays ["ID","NAME","REGION","EMAIL"] & ["1234","John Doe","West","johndoe@mydomain.com"]
    $.each(value,function(innerKey,innerValue){
         console.log(innerValue); //displays 'id','value'....,'1234',..and so on
    });
});

Respondido 19 Jul 14, 12:07

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