如何使用jQuery通过基本身份验证安全地使用soapweb服务(.asmx)



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

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

我一直没能在谷歌上找到任何答案。有可能吗?

好吧,终于解决了这个问题:)
我在错误的方向上搜索,我在寻找如何使用$.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);

为此,您需要使用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>

你可以看看这个教程。

通过jquery ajax调用asmx web服务

最新更新