How to call webservice using javascript

Dr. Rajesh Rolen picture Dr. Rajesh Rolen · Feb 23, 2011 · Viewed 24.7k times · Source

I am using below code to call webservice using javascript:

<head>
    <title>Untitled Page</title>
       <script type="text/javascript" language="JavaScript">
      service = document.getElementById("ViewLink");

     function InitializeService(){
      service.useService('http://myURL/cloudtalk/cloudservice.asmx?wsdl',"CloudService");
     }
     var regtype, regvalue;
     function registerUser(){
      regtype = document.getElementById("Text1").value;
      regvalue =document.getElementById("Text2").value;

      service.CloudService.callService("register", regtype, regvalue);
     }
     function ShowResult(){
    alert(event.result.value);
      }
     </script>
</head>
<body  onload="InitializeService()" id="service" >
<a id="ViewLink"   href="http://"  onclick="registerUser()"  style="behavior:url(webservice.htc);" onresult="onmyresult();"> <font color=maroon>Call</font></a>
  <input id="Text1" type="text" />
        <input id="Text2" type="text" />
        <input id="Button1" type="button" value="Save" onclick="registerUser();" />     
</body>

But i am getting error:"The url is not valid and cannot be loaded"

i done the above code with help of below urls but i checked both samples in these url are not working:

http://www.codeproject.com/KB/webservices/callWebServiceUsingJS.aspx

http://www.codeproject.com/KB/webservices/CallWebServiceFromHtml.aspx

NOTE: my web service is live and i am able to consume it using asp.net but i am facing issue for using it in javascript.

Please help me. Thanks

Answer

maerics picture maerics · Feb 23, 2011

If the web service URL has a different host (by domain name or port) than the one from which the HTML page is served then your JavaScript is violating the same origin policy and any browser will prevent the web service request from loading.