使用jQuery Ajax调用Webservice

时间:2013-11-13 13:35:28

标签: jquery ajax web-services access-denied

我试图用jquery.ajax调用我的web服务。

jQuery.support.cors = true;
    $.ajax({
        type: 'POST',
        url: wsUrl,
        contentType: "text/xml; charset=utf-8",
        dataType: "xml",
        cache: false,
        crossDomain: true,
        data: soapRequest,
        success: reqSuccess,
        error: reqError
    });

我得到“拒绝访问” - 错误和状态/ readyState 0。

如果我使用SoapUI向我的网络服务提出请求,它的效果非常好。

1 个答案:

答案 0 :(得分:1)

在发出SOAP请求时,请确保将processData设置为false以防止jQuery将您的XML请求转换为字符串。

$.ajax({
    type: 'POST',
    url: wsUrl,
    contentType: "text/xml; charset=utf-8",
    dataType: "xml",
    cache: false,
    crossDomain: true,
    data: soapRequest,
    processData: false,
    success: reqSuccess,
    error: reqError
});

来自文档:http://api.jquery.com/jQuery.ajax/

processData (default: true)
Type: Boolean
By default, data passed in to the data option as an object (technically, anything 
other than a string) will be processed and transformed into a query string, fitting 
to the default content-type "application/x-www-form-urlencoded". If you want to send 
a DOMDocument, or other non-processed data, set this option to false.