PDA

View Full Version : Parse JSONP response



siebmanb
9 Oct 2011, 12:19 PM
I found articles on how to parse JSON data (using Ext.decode) but I couldn't find anything about parsing JSONP data (which should be quite similar).

For example, I need to explore the data in the variable "data" :


getData: function(endpoint,action,params,src)
{
Ext.util.JSONP.request({
url: theurl,
callbackKey: 'callback',
callback: function(data) {
// I want to explore data
},
});
},


Thank you very much people !

vela1606
9 Oct 2011, 8:08 PM
This is not tested! But I think this is it..


Ext.util.JSONP.request({
url: theurl,
callbackKey: 'callback',
callback: function(data) {
var foo = Ext.JSON.decode(data.responseText);
// I think is responseText
// this json string {"success":true, "msg":"Hello World!"}
// should alert Hello World!
alert(foo.msg);
},
})


if this doesn't work just console.log(data), look inside the data object and find where the json string is

siebmanb
10 Oct 2011, 1:21 AM
Thanks for your help. The code you offered doesn't work.

TypeError: 'undefined' is not an object (evaluating 'Ext.JSON.decode')

However, logging the object in the console was a great idea (didn't know this was possible). I can indeed see what's inside the packet. For example, I have a field called 'name'. How can I extract it ?

vela1606
10 Oct 2011, 3:51 AM
are you using ExtJs 4? if not then you are posting in the wrong forum :-)

but anyways.. if you can see the object with console.log() all you need to do is navigate within the obj using '.' dots

http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)

not sure how your callback is constructed but should be something like this data.name

siebmanb
10 Oct 2011, 5:48 AM
Damn, wrong forum, sorry guys. The following command gives me 1.1.0

alert(Ext.version)


Really ? Version 1 ? I used the version packed within Sencha Touch.

I already tried data.name, it doesn't work. I will go post in the right forum then !

Thank you all

siebmanb
12 Dec 2011, 8:25 AM
To explore the data, you can do for example :
data[0].fieldname

Using console.log(date) you can find out if it is an array and then add the [index].