JQuery AJAX 使用 SOAP Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7013111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:41:33  来源:igfitidea点击:

JQuery AJAX Consume SOAP Web Service

jqueryajaxsoap

提问by Andy Evans

I have been experimenting and trying to learn JQuery, using AJAX to consume a SOAP web service I had written some time ago. Below is the code I am using:

我一直在试验和尝试学习 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>

Currently, the method being called in the web service returns an array of category families that contain a category code and a category description. Since the method returns XML, I set the ajax query accordingly. However, when I run the app, I get an 'error' alert box - I am sure what would be causing the problem. I know the web service works, it's called several hundred times a day by other .NET web apps that I've written.

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

Any help would be greatly appreciated.

任何帮助将不胜感激。

Thanks,

谢谢,

回答by Mrchief

Try setting processData: falseflag. This flag is trueby default and I guess jQuery is convertingyour XML to string.

尝试设置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
});