Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
Hiii,
 
I want to call web service from (javascript html) code, i tried lots of links,
but its not working in my case, i have a very simple web service running on http://wsjs.sudesi.in/WebService.asmx[^] which return hello world. if somebody can use same link and give me output using (javascript and html), it will be greatly helpfull for me.
 
Thanks in advance
Posted 12-Dec-12 18:52pm
Edited 12-Dec-12 18:54pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<script language="javascript" type="text/javascript">
 
var soapHeader = '<?xml version="1.0" encoding="utf-8"?>'
 
soapHeader += '<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/">'
 
soapHeader += '<soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>'
 
//the url of Webservice

var postUrl = 'http://localhost:4174/TestWeb/WebService.asmx';
 

var soapActionUrl = 'http://tempuri.org/HelloWorld';
 

var xmlhttp = null;
try {
 
xmlhttp = new XMLHttpRequest();
}catch (e) {xmlhttp = false; }
 
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
 
}catch (e) { xmlhttp = false; }
try {
 
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');}catch (e) { xmlhttp = false; }
alert(xmlhttp);
 
if( xmlhttp )
{
 
xmlhttp.open ('POST', postUrl, true);
 
xmlhttp.onreadystatechange = function()
{
 
if( xmlhttp.readyState == 4 )
{
 
alert(xmlhttp.responseText);
 
}
 
};
 

 
xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
 

xmlhttp.setRequestHeader("Host", "localhost");
 
xmlhttp.setRequestHeader("SOAPAction", soapActionUrl); xmlhttp.setRequestHeader("Content-Length", soapHeader.length );
 

xmlhttp.send(soapHeader);
 
}
 
</script>
 
Nice article you can fallow this Invoking WebServices using JavaScript[^]
  Permalink  
Comments
sunil_mali at 13-Dec-12 6:42am
   
hi friend,

i have tried your code, but this code is only working in ie, i am developing this solution to work on mobile and there are very less mobiles which has internet explorer inbuilt.
I think problem is with .htc file which you have provided with your code.
Do you have any other solution, your solution didn't worked for me as it works only on ie, and it was too complecated.

Calling Web Services from HTML Pages using JavaScript[^]

i checked above link, and it helped me a lot to run this solution
but i should make it run on all browsers because my application is mobile based.
can we do same think without .htc file?
do u have any other solution for same?
will it work on all devices and all browser?
Suvabrata Roy at 13-Dec-12 7:41am
   
Try this
http://dojotoolkit.org/reference-guide/1.7/dojo/xhrGet.html
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

ha ha...
finally i found it...
i used jquery to retrieve my data....
<html>
 <head>
    <title>Calling Web Service from jQuery</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {
                var wsUrl = "http://mobexwebservice.sudesi.in/mobexchange.asmx?op=Hello";
 
                var soapRequest =
'<?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> \
    <Hello xmlns="http://tempuri.org/"> \
      <name>' + $("#txtName").val() + '</name> \
    </Hello> \
  </soap:Body> \
</soap:Envelope>';
 
                $.ajax({
                    type: "POST",
                    url: wsUrl,
                    contentType: "text/xml",
                    dataType: "xml",
                    data: soapRequest,
                    success: processSuccess,
                    error: processError
                });
 
            });
        });
 
        function processSuccess(data, status, req) {
            if (status == "success")
                $("#response").text($(req.responseXML).find("HelloResult").text());
        }
 
        function processError(data, status, req) {
            alert(req.responseText + " " + status);
        }
 
    </script>
</head>
<body>
    <h3>
        Calling Web Services with jQuery/AJAX
    </h3>
    Enter your name:
    <input id="txtName" type="text" />
    <input id="btnCallWebService" value="Call web service" type="button" />
    <div id="response" />
</body>
</html>
  Permalink  
Comments
raaz.rbs at 7-Nov-13 11:48am
   
this solution is not working with Google Chrome!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100