如何在 javascript 或 Jquery 中调用 SOAP (XML) webservice?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11815985/
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-10-26 14:26:21  来源:igfitidea点击:

How to call SOAP (XML) webservice in javascript or Jquery?

javascriptjquery

提问by Sohail Ahmad

I'm trying to call asp.net webservice in javascript / Jquery, i have tried so many examples but unfortunately not succeeded,

我正在尝试在 javascript/Jquery 中调用 asp.net webservice,我尝试了很多示例但不幸的是没有成功,

here is the code which currently i'm trying,

这是我目前正在尝试的代码,

    login("[email protected]", "123456");
    var productServiceUrl = 'http://localhost:50575/Service1.asmx?op=test'; // Preferably write this out from server side

    function login(Email, Password) {
        var soapMessage = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \
<soap:Body> \
<login xmlns="http://test.com/"> \
<Email>' + Email + '</Email> \
<Password>' + Password + '</Password> \
</login> \
</soap:Body> \
</soap:Envelope>';

        $.ajax({
            url: productServiceUrl,
            type: "GET",
            dataType: "xml",
            data: soapMessage,
            complete: endSaveProduct,
            error: function (a, b, c) {
                alert(a + "\n"  + b + "\n" + c);
            },
            contentType: "text/xml; charset=\"utf-8\""
        });

        return false;
    }

    function endSaveProduct(xmlHttpRequest, status) {
        $(xmlHttpRequest.responseXML)
    .find('loginResult')
    .each(function () {
        alert( $(this).find('Message').text());
    });
    }

please help me out, Thanks in advance.

请帮助我,提前致谢。

回答by Esailija

There are multiple issues:

有多个问题:

  • You are sending a request to different domain so it won't work unless that domain is sending Cross-origin resource sharing (CORS) headers Access-Control-Allow-Origin: *or specifically allowing your origin
  • You are using GETwhere as you should be using POST, because in SOAP over HTTP the envelope must be in the request body.
  • jQuery always thinks your data is application/x-www-form-urlencodedunless you set processDatato false. Only setting contentTypewill just make the header lie and doesn't actually change this.Actually this is not true if the dataparameter is a string.
  • 您正在向不同的域发送请求,因此除非该域正在发送跨域资源共享 (CORS) 标头Access-Control-Allow-Origin: *或特别允许您的源,否则它将无法工作
  • 您正在使用GETwhere 应该使用的地方POST,因为在 HTTP 上的 SOAP 中,信封必须在请求正文中。
  • jQuery 总是认为你的数据是,application/x-www-form-urlencoded除非你设置processData为 false。只有设置contentType只会使标题撒谎,实际上并不会改变这一点。实际上,如果data参数是字符串,则情况并非如此。

It appears that your target domain is not allowing CORS, so it is impossible to do it directly from client-side. You must use a server proxy to do the request.

您的目标域似乎不允许 CORS,因此无法直接从客户端执行此操作。您必须使用服务器代理来执行请求。

If they allowed CORS, you would do it like so:

如果他们允许 CORS,你会这样做:

var soapMessage = '<?xml version="1.0" encoding="utf-8"?>\
                    <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>\
                        <login xmlns="http://tastygo.com/">\
                          <BBMID>string</BBMID>\
                          <Email>string</Email>\
                          <Password>string</Password>\
                        </login>\
                      </soap12:Body>\
                    </soap12:Envelope>';

$.ajax( "http://m.allaccesstnt.com/AATnTWebservices/Webservices/Userwebservice.asmx", {

    contentType: "application/soap+xml; charset=utf-8",
    type: "POST", //important
    dataType: "xml",
    data: soapMessage

});

But this will not work because the server does not allow OPTIONS, which the browser must use to determine whether a cross-origin request is allowed:

但这不起作用,因为服务器不允许 OPTIONS,浏览器必须使用它来确定是否允许跨域请求:

OPTIONS http://m.allaccesstnt.com/AATnTWebservices/Webservices/Userwebservice.asmx 405 (Method Not Allowed)

The second problem is:

第二个问题是:

XMLHttpRequest cannot load http://m.allaccesstnt.com/AATnTWebservices/Webservices/Userwebservice.asmx. Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.

回答by Jugal Choudhary