【问题标题】:How to consume soap web service (.asmx) secure by basic authentication using jQuery?如何通过使用 jQuery 的基本身份验证安全地使用肥皂网络服务 (.asmx)?
【发布时间】:2013-12-24 10:06:17
【问题描述】:

我正在尝试调用一个 (.asmx) 肥皂网络服务,该服务需要使用 jQuery(或任何可行的方法)进行基本身份验证。

以及如何将参数传递给(.asmx)soap web 服务

我无法在 Google 上找到任何答案。有可能吗?

【问题讨论】:

标签: javascript jquery web-services soap asmx


【解决方案1】:

好的,我终于能够解决这个问题了:)
我在寻找错误的方向,我在寻找如何使用 $.Ajax 打开受基本身份验证保护的 URL,我应该使用 XMLHttpRequest() 从 JavaScript 搜索消费 SOAP 服务
以下是我的问题的答案:

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】:

    您需要为此使用 jquery ajax。

                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 + ')');
    
                    },
                });
    

    您还需要为该 asmx 文件添加服务引用。

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

    你可以看看这个教程。

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

    【讨论】:

    • 我没有服务器代码(如 ASP.NET)来帮助我处理和使用服务,我正在尝试创建一个只使用 JavaScript 的超级应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多