简体   繁体   English

如何在JavaScript或Jquery中调用SOAP(XML)Web服务?

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

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网络服务,我尝试了很多示例,但不幸的是没有成功,

here is the code which currently i'm trying, 这是我目前正在尝试的代码,

    login("abc@gmail.com", "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. 请帮帮我,谢谢。

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 您正在将请求发送到其他域,因此除非该域发送跨域资源共享(CORS)标头Access-Control-Allow-Origin: *或明确允许您的来源,否则该请求将不起作用
  • You are using GET where as you should be using POST , because in SOAP over HTTP the envelope must be in the request body. 您正在使用GET ,而应该使用POST ,因为在基于HTTP的SOAP中,信封必须位于请求正文中。
  • jQuery always thinks your data is application/x-www-form-urlencoded unless you set processData to false. jQuery始终认为您的数据是application/x-www-form-urlencoded除非您将processData设置为false。 Only setting contentType will just make the header lie and doesn't actually change this. 仅设置contentType只会使标头撒谎,并且实际上不会更改此标头。 Actually this is not true if the data parameter is a string. 实际上,如果data参数是一个字符串,则不是这样。

It appears that your target domain is not allowing CORS, so it is impossible to do it directly from client-side. 您的目标域似乎不允许使用CORS,因此不可能直接从客户端进行。 You must use a server proxy to do the request. 您必须使用服务器代理来执行请求。

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来确定是否允许跨域请求:

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.

只需添加http://m.allaccesstnt.com/AATnTWebservices/Webservices/Userwebservice.asmx/ 测试并在Web服务器端添加标头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM