Call a web service from javascript

I wrote a web service in ASP.NET, it has this address:

http://localhost/RouteGen/Service.asmx 

The web service has a GetMessage web method, it takes no parameters and returns a string.

Everything is fine with the web service, I call its methods from other ASP.NET applications or even from the Android application.

Server Code:

 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { [WebMethod] public string GetMessage() { return "Hello World"; } } 

Now I need to call the GetMessage web method from javascript.

html page: (on this web page there is no connection with the web service code, this is a completely different project! You can assume that it is written in notepad win)

 ... <body id="body1" onload="initialize()" style="behavior:url(webservice.htc)"> </body> ... 

in the initialize () method, which I call:

 ... service_init(); processResult(); 

And there are the following functions:

 function service_init() { body1.useService("http://localhost/RouteGen/Service.asmx?WSDL","TheService"); body1.TheService.callService("GetMessage"); } function processResult(result) { alert(result); } 

So, I have a:

1) In IE, processResult() returns "undefined"

2) In Chrome and FireFox, it does not work at all (a simple notification after using the service does not appear)

Where is the problem? How to make javascript call a web method as usual and from different browsers?

+4
source share
2 answers

In the Aspx section

Add the ScriptManager tag as follows:

  <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference path="~/sample.asmx"/> </Services> </asp:ScriptManager> 

In JavaScript, call the web service (sample.asmx) as follows:

 <script language="javascript" type="text/javascript"> function CalledOnAnyClientClickEvent() { var parameter1="dsfsfs"; NameSpace1.WebService1.HelloWorld(parameter1,OnSucess,OnFail); } function OnSuccess(asd) { alert(asd);//result will contain the return parameter from web service } function OnFail(asd) { alert(asd); } </script> 

See the Asmx section (sample.asmx) below,

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Serialization; using System.Web.Script.Services; namespace NameSpace1 { /// <summary> /// Summary description for WebService1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld(string par1) { //do your logic here return "Hello World"; } } } 

Hope this helps ...

+5
source

ASMX is a SOAP web service. SOAP is relativistically complex.

The best way to return data to the browser is to use REST. REST services can be used with JQUERY.

You can create WCF services that use REST and return a JSON result.

If your service is not located on the same server as your web page, you will need to use something like JSONP to call the cross domain.

+2
source

All Articles