JQuery AJAX使用SOAP Web服务

时间:2011-08-10 15:08:51

标签: jquery ajax soap

我一直在尝试和尝试学习JQuery,使用AJAX来使用我之前写过的SOAP Web服务。以下是我正在使用的代码:

<script type="text/javascript">
    var webServiceURL = 'http://local_server_name/baanws/dataservice.asmx?op=GetAllCategoryFamilies';
    var soapMessage = '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><GetAllCategoryFamilies xmlns="http://tempuri.org/" /></soap12:Body></soap12:Envelope';

    function CallService()
    {
        $.ajax({
            url: webServiceURL, 
            type: "POST",
            dataType: "xml", 
            data: soapMessage, 
            contentType: "text/xml; charset=\"utf-8\"",
            success: OnSuccess, 
            error: OnError
        });

        return false;
    }

    function OnSuccess(data, status)
    {
        alert(data.d);
    }

    function OnError(request, status, error)
    {
        alert('error');
    }

    $(document).ready(function() {
        jQuery.support.cors = true;
    });
</script>

<form method="post" action="">
    <div>
        <input type="button" value="Call Web Service" onclick="CallService(); return false;" />
    </div>
</form>

目前,在Web服务中调用的方法返回包含类别代码和类别描述的类别系列数组。由于该方法返回XML,因此我相应地设置了ajax查询。但是,当我运行应用程序时,我收到一个“错误”警告框 - 我确定会导致问题的原因。我知道Web服务有效,我写的其他.NET Web应用程序每天都会调用几百次。

非常感谢任何帮助。

谢谢,

1 个答案:

答案 0 :(得分:23)

尝试设置processData: false标记。默认情况下,此标志为true,我猜jQuery是converting您的XML字符串。

$.ajax({
    url: webServiceURL, 
    type: "POST",
    dataType: "xml", 
    data: soapMessage, 
    processData: false,
    contentType: "text/xml; charset=\"utf-8\"",
    success: OnSuccess, 
    error: OnError
});
相关问题