How to consume soap web service (.asmx) secure by basic authentication using jQuery?

霸气de小男生 提交于 2021-01-29 21:40:27

问题


I am trying to call a (.asmx) soap web service that require basic authentication using jQuery (or anything that would work).

And how to pass parameters to the (.asmx) soap web service

I've not been able to come up with any answers on Google. Is it possible?


回答1:


OK, Finally I was able to resolve this issue :)
I was searching in the wrong direction, I was looking how to open the URL secured by basic authentication using $.Ajax, where I should search for consuming SOAP service from JavaScript using XMLHttpRequest()
the following is the answer to my question:

var symbol = "MSFT"; 
var xmlhttp = new XMLHttpRequest();
//xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote", true);
// if you use username and password to secure your URL
xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote", true, 'username', 'password');
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        console.log(xmlhttp.responseText);
    }
}
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
          '<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> ' +
          '<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
          '<symbol>' + symbol + '</symbol> ' +
          '</GetQuote> ' +
          '</soap:Body> ' +
          '</soap:Envelope>';
xmlhttp.send(xml);



回答2:


You need to use jquery ajax for that.

            var uri="http://asmx_file_path/asmx_service_file_name/method_name_in_asmx_file"
            //ex: var uri = "http://mysite.test.com/services/data/mobile.asmx?method=login&username=" + uname+ "&password=" + pwd;
            $.ajax({
                type: "GET",
                url: uri,
                success: function (msg) {
                    jasondata = eval('(' + msg + ')');

                },
            });

You also need to add service reference for that asmx file.

    <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/services/data/mobile.asmx" />
    </Services>
    </asp:ScriptManagerProxy>

You can have a look at this tutorial.

calling-asmx-web-service-via-jquery-ajax



来源:https://stackoverflow.com/questions/20758899/how-to-consume-soap-web-service-asmx-secure-by-basic-authentication-using-jqu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!