I'm developing an app on PhoneGap (Cordova Apache) which make an Jquery Ajax XmlHttpRequest cross-domain request to my server.
Here is the code :
/*
* Create the XMLHttpRequest instance
*/
function getXDomainRequest() {
var xdr = null;
if (window.XDomainRequest) {
xdr = new XDomainRequest();
} else if (window.XMLHttpRequest) {
xdr = new XMLHttpRequest();
} else {
alert("Votre navigateur ne gère pas l'AJAX cross-domain !");
}
return xdr;
}
/*
* Load the data from menu into XMLHttpRequest and call create menu
*/
function sendData() {
var xdr = getXDomainRequest();
xdr.onload = function() {
var dcat = $.parseJSON(xdr.responseText);
createMenu(menu, dcat);
configureMenuScript();
}
xdr.open("GET", "http://preview.********.com/api/ApiProductList");
xdr.send();
}
sendData();
Its working fine on Firefox 28, IE 11 and Chrome 34. It also work fine on the android emulator with an AVD android 4.0.
In the Manifest.xml file I put the following data
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
And in the Config.xml
<param name="android-package" value="org.apache.cordova.core.NetworkManager" />
<access origin="*" />
I'm also allowing the cors in the above JS script
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
This should allow the Cross-Domain.
-> The issue is that it doesn't work on my device. (Android 4.2.2) I mean, the app load well, but when it come to display the data out of the ajax request, it doesn't work anymore.
Maybe I'm missing an authorization in the manifest or the config file ?
Unfortunately I can't debug live on the device :s
Thanks in advance !
nb: Windows 8.1 - PhoneGap Cordova Apache 3.4
I found out my problem. It was actually not due to an code error but due to the firewall of the company.
So, basically, the code above is working. At least it is a great recap on how to implement a XMLHttpRequest on phonegap :)