0

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?

Owidat
  • 1,071
  • 2
  • 15
  • 20
  • Is it about **ajax**? – Jai Dec 24 '13 at 10:14
  • Yes I need to use ajax. I Almost found a solution but I did not try it yet, this link show how to call the service from (jQuery) but I need to make changes on the service layer: http://stackoverflow.com/questions/15342651/how-to-consume-a-asmx-web-service-with-jquery – Owidat Dec 25 '13 at 02:48
  • , and this show how to call a secure service but this is using different and newer web service technology which is json: http://stackoverflow.com/questions/6025764/how-do-you-call-a-json-web-service-that-requires-basic-authentication-using-jq – Owidat Dec 25 '13 at 02:53
  • and this to understand how to pass parameters to the service: http://stackoverflow.com/questions/16919404/how-to-understand-what-xml-to-send-to-an-asmx-web-service I need to combine the 3 solutions – Owidat Dec 25 '13 at 02:54

2 Answers2

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);
Owidat
  • 1,071
  • 2
  • 15
  • 20
0

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

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
  • I do not have a server code (Like ASP.NET) to help me process and consume the service, I'm trying to create a hyper app where it will use only JavaScript. – Owidat Dec 25 '13 at 02:58