PDA

View Full Version : scripttag vs ajax



danvega
23 Jul 2010, 5:21 PM
I am working on a list and the local version of the example is working just fine..



this.topics = new Ext.List({
id:'topicsList',
fullscreen:true,
tpl:'<tpl for="."><div class="topic"><strong>{name}</strong></div></tpl>',
singleSelect:true,
itemSelector:'div.topic',
dockedItems: [this.topicsTitlebar],
store: new Ext.data.Store({
model: 'Topic',
sorters: 'name',
autoLoad:true,
groupField: 'name',
proxy: {
url:'data/topics.txt',
type: 'ajax',
reader: 'json'
}

})
});


If I change the url to an outside url and the type to scripttag it doesn't work. I get no errors but the data just does not load. I did a manual request and copied the json result to data/topics.txt to make sure the data is good and it is. Why would the scripttag proxy not work?

danvega
23 Jul 2010, 6:46 PM
I also tried to create my own local proxy using a server side class



this.topics = new Ext.List({
id:'topicsList',
fullscreen:true,
tpl:'<tpl for="."><div class="topic"><strong>{name}</strong></div></tpl>',
singleSelect:true,
itemSelector:'div.topic',
dockedItems: [this.topicsTitlebar],
store: new Ext.data.Store({
model: 'Topic',
autoLoad: true,
proxy: {
url:'ajaxProxy.cfc?method=get&targeturl=' + escape('http://abc.com/topics.json'),
type: 'ajax',
reader: 'json'
}
})
});


When I ran that I got the following error in chrome

Uncaught SyntaxError: Unexpected token ILLEGAL

I just can't seem to be able to make a cross domain request and its essential to this app

meyerovb
24 Jul 2010, 12:39 PM
I think what ur looking for is called jsonp, I use jquery for all my Ajax stuff so not sure how to write it in sencha at the moment, but there is a jsonp reader in the documentation so I'm sure you can find an example.

danvega
24 Jul 2010, 12:54 PM
This is right from the ScriptTagProxy class docs

Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain of the running page, you must use this class, rather than HttpProxy.

This is why I thought all I would have to do is change the proxy type, makes sense...but doesn't work

danvega
24 Jul 2010, 1:02 PM
Plus I have tried every single variation of the twitter example on mine and nothing.. Meanwhile this works fine for me..




this.store = new Ext.data.Store({
autoLoad: true,
model: "Tweet",
proxy: {
type: 'scripttag',
url: 'http://search.twitter.com/search.json',
extraParams: {
q: 'cfunited',
rpp: 50,
suppress_response_codes: true
},
reader: {
type: 'json',
root: 'results'
}
}
});

meyerovb
24 Jul 2010, 1:12 PM
When u do cross domain Ajax requests the data needs to come back in jsonp format, a callback method wrapping the json object. You can't just return a straight up json object. Run fiddler against the Twitter example and you'll see that what Twitter returns is not {object} but callbackFunction({object}). Read up on jsonp to understand why.

danvega
24 Jul 2010, 3:26 PM
Makes total sense now! Thanks for the help

epiphanydigital
27 Jun 2011, 11:38 AM
This same issue is driving me nuts and I don't follow the response... when I go to that url (for twitter listed above) in a browser I get a NON-wrapped version (below is a copy/paste). I'm also using a Yahoo service that works fine so long as the results are on Yahoo's server (again, not wrapped), but when I copy the result data to a txt file on my own server (and with my app on my local computer) it fails completely. I've tried wrapping in a function as well, and then I just get "undefined function 'mycallbackfunction'" errors. Someone please help this Sencha n00b!



{
"results":[],
"max_id":85407040721256448,
"since_id":0,
"refresh_url":"?since_id=85407040721256448&q=%27cfunited%27",
"results_per_page":50,
"page":1,
"completed_in":0.160584,
"since_id_str":"0",
"max_id_str":"85407040721256448",
"query":"%27cfunited%27"}

bclinton
27 Jun 2011, 12:38 PM
Dude, slow down on the thread posting. You don't need to post the same question in multiple threads. I replied in one of your other threads: http://www.sencha.com/forum/showthread.php?138533-quot-Uncaught-SyntaxError-Unexpected-token-quot-when-using-JSONP-and-scripttag