通过jQueryAJAX的SOAP请求

SOAP request through jQuery AJAX

本文关键字:请求 SOAP jQueryAJAX 通过      更新时间:2023-09-26

我有一个运行服务的服务器,我想每隔一段时间运行一个对服务的ping请求,这样我就可以知道它什么时候准备好了。

得到以下ping.dat文件:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:dvt="[private]">
    <soapenv:Header />
    <soapenv:Body>
        <dvt:Ping/>
    </soapenv:Body>
</soapenv:Envelope>

以及以下Javascript函数(将包含在setInterval()函数中):

function doAjax() {     
    //load request document
    $.ajax({
        cache: false,
        crossDomain: true,
        async: true,                
        dataType: 'xml',
        type: 'POST',
        data: null,
        url: "./ping.dat",
        error: function(xhr, sta, err){ alert(err); },
        success: function(ret, sta, xhr){
            //ping service
            $.ajax({
                cache: false,
                crossDomain: true,
                async: false,
                processData: false,
                contentType: "text/xml; charset='"UTF-8'"",
                dataType: 'xml',
                data: processXML(xhr.responseText),
                type: 'POST', 
                url: "[private]",
                error: function(xhr, sta, err){ 
                    alert(err); 
                },
                success: function(ret, sta, xhr){ 
                    $('#response').text($.trim(ret)); 
                },
                complete: function(xhr, sta){
                    alert('complete');
                },
            });
        }
    });
}
function processXML(text){
    var ret = null;
    if ((typeof(ret) !== 'undefined')&&(text !== null)&&(text.length !== 0))
        ret = $.trim(text.replace(/['n't]+/g, ''));
    
    return ret;
}

当我使用SoapUI调用服务并加载ping请求时,它就工作了。

当我使用JS函数时,浏览器报告:

OPTIONS[private]200(OK)jquery-1.10.2.js:8706

XMLHttpRequest无法加载[private]。请求的资源上不存在"Access Control Allow Origin"标头。原点'http://localhost:8080因此不允许访问。

是什么原因造成的?

消息很清楚:请求的资源上不存在"Access Control Allow Origin"标头。

如果您确实在执行跨域Ajax请求,那么服务器必须使用适当的HTTP头进行响应。浏览器发出OPTIONS HTTP请求,并通过查看接收到的标头来检查服务器是否"批准"访问。如果标头丢失,则浏览器有义务返回错误并禁止对资源的请求。

有关详细信息,请参阅此处:HTTP访问控制(CORS)

SoapUI不像浏览器那样受到同源安全策略的影响,所以从SoapUI ping web服务是有效的。