PDA

View Full Version : Problem with cross domain in AJAX call



nattawat
8 Oct 2013, 12:26 AM
Hi,
I am currently using Sencha touch 2.3.0 with Phonegap to create an Android app. I install it using the command "sencha phonegap init".

In my app, I create a proxy store to load data from a remote server. It works properly when I run it on Chrome using Ripple extension to allow remote cross domain proxy. However, it does not work when I deploy it on either en emulator or a real device using command "sencha app build -run native". I have read from other blogs that we can add access in config.xml file but it still does not work for me.

Here is my code for the Store. I am not sure what I did wrong. Any suggestion?



Ext.define('App.store.Courses', {
extend: 'Ext.data.Store',
requires: ['App.model.Course'],
config: {
model: 'App.model.Course'
},
proxy: {
type: 'ajax',
url: 'http://mysite.com/webservice',
pageParam: false, //to remove param "page"
startParam: false, //to remove param "start"
limitParam: false, //to remove param "limit"
noCache: false, //to remove param "_dc"
method: 'GET',
withCredentials: true,
useDefaultXhrHeader: false,
reader: {
type: 'json',
}
}
});


And I already add the lint below in config.xml

<access origin="*" />

fmoseley
9 Oct 2013, 12:54 PM
Maybe a bug in phonegap. What version are you using?

nattawat
9 Oct 2013, 7:14 PM
My phonegap version is 3.0.0-0.14.4.

I tried using AJAX written using plain JS and it works. So my hot fix is to create Store, use normal AJAX and then assign data to the store. However, it would be very nice if I can set up proxy for the store.

Here is my hot fix for now!


var commondStore = Ext.create('Ext.data.Store', {
model: rmodel
});

var ajax = new XMLHttpRequest();
var url = 'http://mysite.com/webservice'

ajax.open("GET", url, true);
ajax.send();

ajax.onreadystatechange = function(){

if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
commondStore.setData(ajax.responseText);
callback(commondStore);
}
}

shenglinxu
9 Jul 2014, 6:44 AM
Tanks nattawat!
use $.ajax works for me:

var store = list.getStore();
$.ajax({
url: url,
type: "GET",
dataType: "json",
success:function(data) {
//alert(data + " data ");
store.setData(data);
}