PDA

View Full Version : Ext.data.JsonP request Unexpected token :



vandzi
29 Mar 2012, 7:40 AM
Hi folks.
I want to make call to authenticate user and get back token in json file. I use Ext.data.JsonP but when I call it display Uncaught SyntaxError: Unexpected token : in browser.
I can't use Ext.Ajax because it's on another domain.
Here is code:


Ext.data.JsonP.request({
url : 'myurl',


success : function(result) {


alert('success');
},
failure : function(result) {
alert('failure');
},
callback : function(result) {
alert('callback');
}
});



And this is expected result:

{"token":"MTAwNTI7V00yWkdVMFlqZDtkMDExZWU4YmU4ZTdlM2RjYzNkYzU3OThhYWViYjk4YmMxMzY2NTY2"}

Can somebody tell me what's causing Unexpected token : error?
Or other way how to get response.
Thanks

mitchellsimoens
29 Mar 2012, 8:08 AM
{"token":"MTAwNTI7V00yWkdVMFlqZDtkMDExZWU4YmU4ZTdlM2RjYzNkYzU3OThhYWViYjk4YmMxMzY2NTY2"}

This response is not valid JSONP. It needs to be wrapped by a callback function.

vandzi
29 Mar 2012, 8:21 AM
Thanks. So how can I get this response if it's not valid jsonp and Ext.Ajax.request is not working because of cross domain violation?

arthurakay
29 Mar 2012, 12:03 PM
I don't want to sound snarky, but can you ask the remote server to give you a valid JSONP response?

I'm assuming you don't have control over that remote server, but some web services allow you to choose a response type (JSON, XML, JSONP, etc)

Another option would be to create a server-side proxy on your domain. Have this proxy call the remote server for you... then you can use Ext.Ajax to connect to your local domain proxy.

vandzi
29 Mar 2012, 1:51 PM
How is it possible that this doesn't work because it's not valid JsonP file but example from sencha works


// Make the JsonP request
Ext.data.JsonP.request({
url: 'http://free.worldweatheronline.com/feed/weather.ashx',
callbackKey: 'callback',
params: {
key: '23f6a0ab24185952101705',
q: '94301', // Palo Alto
format: 'json',
num_of_days: 5
},
success: function(result) {
// Get the weather data from the json object result
var weather = result.data.weather;

}
});

Data are not valid JsonP file but only Json.
You can see data at http://free.worldweatheronline.com/feed/weather.ashx?key=23f6a0ab24185952101705&q=94301&num_of_days=5&format=json

evant
29 Mar 2012, 6:02 PM
The example code also sends a callback key.

Look here: http://free.worldweatheronline.com/feed/weather.ashx?key=23f6a0ab24185952101705&q=94301&num_of_days=5&format=json&callback=foo