1

我正在开发一个必须从 Microsoft Dynamics NAV SOAP Web 服务加载数据的应用程序。当我在浏览器(IE9 或 Chrome v 26.0...)中运行代码时,代码会执行应有的操作。但是当我在 android 模拟器或真实设备(htc 感觉)上测试代码时,ajax 调用失败并且我得到 XMLHttpRequest 错误 0。

但是,我也可以从不同的 web 服务加载数据,而无需在 android 系统上进行基本身份验证。

我的白名单(位于 res/xml/config.xml)如下所示:

<access origin="*" />

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

这里是代码:

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:
}

有人知道如何解决这个问题吗?

使用的设置:jquery.mobile-1.3.0 jquery-19.1 cordova-2.5

4

1 回答 1

1

要使 CORS 工作,您的 Web 服务必须启用 CORS。这是一篇很好的文章。如果您无法通过 CORS 启用您的服务,则另一种选择是使用 JSONP 而不是 JSON。

于 2013-04-17T11:09:54.003 回答