使用JQuery AJAX消费SOAP Web服务

12

我一直在尝试学习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应用程序每天都会调用它数百次。

非常感谢您的任何帮助。

谢谢,


4
顺带提一下,你的soapMessage变量在soap信封的结尾处缺少最后的">"。 - Kyle
@Kyle,太棒了,就是这样! - Andy Evans
1个回答

24

尝试设置 processData: false 标志。这个标志默认为true,我猜 jQuery 正在 转换 你的 XML 成字符串。

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

2
这个问题已经解决了,是我SOAP消息末尾缺少了">"符号。谢谢! - Andy Evans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接