PDA

View Full Version : jsonp callback method undefined



jeremym
23 Feb 2012, 1:19 PM
I'm trying to use a jsonp proxy (since the ajax proxy doesn't allow for cross-domain calls). When I switched I discovered I was getting back the data I expected, but with an "Invalid Label" error... After poking around google a bit, it looks like the issue is that jsonp response needs to be wrapped in a function... so instead of { "key1" : " value1" , "key2": "value2" } its supposed to be callBackFunct({ "key1" : " value1" , "key2": "value2" }); ... So I took some known output from the server and saved it as a .json file on my localhost... after confirming that it loaded successfully with an ajax proxy, I switched to a jsonp proxy type and wrapped the json in "myCallback(<jsonhere>);" ... When I try to access this via the jsonp proxy I get an error "myCallback is not defined". I'm using the a file for now for testing, but will be able to control how the server wraps the data once I've got the right client side solution... Reading http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.proxy.JsonP (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.proxy.JsonP) makes it sound like the client doesn't have to do anything and that the proxy will handle it, but clearly I'm missing some element.

My store code:

proxy: {
type: 'jsonp',
url: 'data/smalltest.json',
headers: { Authorization : 'Basic YWRtTEMPNOTREALaW4=' },
callbackKey: 'myCallback',
reader: {
type: 'json',
root: 'appMetrics'
}
}
Small snippet of the json:

myCallback({
"query_info": {
"granularity": "5m",
"start": 1329866030000,
"end": 1329866329999
},
"appMetrics": [
{
...
}]
});

mitchellsimoens
23 Feb 2012, 1:22 PM
The response needs to match the callbackKey param. The callbackKey is the name of the parameter sent in the request. The value of that parameter is what the JSON needs to be wrapped in.

jeremym
23 Feb 2012, 1:34 PM
I specified a callbackKey of "myCallback" and the json begins with myCallback(<json>);

I tried adding a callback=myCallback parameter to my url (though it felt silly since its reading it straight from localdisk) in case the browser is scraping it to use... still the same result. I'm clearly interpreting something wrong here :-/

mitchellsimoens
23 Feb 2012, 1:54 PM
The callback parameter will automatically be added to the request

jeremym
23 Feb 2012, 2:08 PM
That makes sense. I appreciate the quick response :) Unfortunately I'm still getting the error... Is there some limitation trying to use a local file with a jsonp proxy (I can't think of a reason this would be true, but I'm running out of ideas)... It seems like as long as I wrap the json on disk in the same function as I specify for a callbackKey that it should work with the local file... I can ask the server side programmer to implement the wrapper now, but I'm hesitant to do so if I can't show that it will work afterwards.

jeremym
24 Feb 2012, 7:38 AM
Server side programmer added the server side bits and it works directly... just not off a local data\xyz.json file... Thanks again for the help

mitchellsimoens
24 Feb 2012, 8:00 AM
Server side programmer added the server side bits and it works directly... just not off a local data\xyz.json file... Thanks again for the help

Glad to hear it is actually working. For a local JSON file, you would need to change it to match your callback param which isn't good for development.