user2289828
user2289828

Reputation:

PhoneGap + jQuery android app; ajax request runs in browser but not on phone or emulator

I am working on an app which has to load data from a Microsoft Dynamics NAV SOAP web service. When I run the code in a browser (IE9 or Chrome v 26.0...) the code does what it should. But when I test the code on an android emulator or a real device (htc sensation) the ajax call fails and I get the XMLHttpRequest error 0.

However, I could also load data from different web-services without basic authentication on android systems.

My whitelist (located in res/xml/config.xml) looks like this:

<access origin="*" />

<plugins>
    ... // all other possible plugins
    <plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
</plugins>

and here the code:

function readTimeRecords(callback) {

var wsUrl = 'http://.../WS/CRONUS%20AG/Page/Time_Recording';

var soapMessage = '<?xml version="1.0" encoding="utf-8"?>' + 
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' + 
    '<soap:Body>' + 
        '<ReadMultiple xmlns="urn:microsoft-dynamics-schemas/page/time_recording" >' +
            '<filter>' +
                '<Field>No</Field>' +
            '</filter>' +
            '<bookmarkKey />' +
            '<setSize>' +
                '5' +
            '</setSize>' +
        '</ReadMultiple>' +
     '</soap:Body> ' + 
 '</soap:Envelope>';

// enable cross site requests in IE
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;

$.ajax({
    url : wsUrl,
    type : "POST",
    dataType : "text",
    data : soapMessage,
    processData : false,
    contentType : "text/xml", 

    password : "passwd",
    username : "domain\\user",

    beforeSend : function(xhr) {
        xhr.setRequestHeader('SOAPAction', 'urn:microsoft-dynamics-schemas/page/time_recording');
    },
    success : function(msg) {
        ...     
        callback(record);
    },
    error : onError 
})  
}

function onError(XMLHttpRequest, textStatus, errorThrown) { 
    debugger;
    console.log("error: " + textStatus + ", errorThrown: " + errorThrown);
        // i just get the message error: error:
}

Does someone know how to fix that issue?

used setup: jquery.mobile-1.3.0 jquery-19.1 cordova-2.5

Upvotes: 1

Views: 2884

Answers (1)

Nishanth Nair
Nishanth Nair

Reputation: 2965

For CORS to work, your web service must be CORS enabled. Here is a good writeup . If you cannot CORS enable your service, using JSONP instead of JSON is the other option.

Upvotes: 1

Related Questions