Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery to call an ASP.NET web service?

I'm trying to use jQuery to get data from an ASP.NET web service (SharePoint Server 2007 lists.asmx), but any call to a web service will really help as a first step in that direction.

like image 475
Simara Avatar asked Oct 23 '08 16:10

Simara


People also ask

How can we make AJAX call in ASP.NET webforms?

Server To Client (ASP.NET MVC) Firstly, add jQuery CDN (Content Delivery Network) reference which loads jQuery library. Second, the code block to get list of employee from Server and display it. Here, this method type is "GET", gets data in success properties as response parameter.

Can we call C# code behind using jQuery?

Can we call C# code behind using jQuery? we can but we need to specify the backend method as static . Yes,But it having some prerequisiteC# method mark as webmethod.


1 Answers

I use this method as a wrapper so that I can send parameters. Also using the variables in the top of the method allows it to be minimized at a higher ratio and allows for some code reuse if making multiple similar calls.

function InfoByDate(sDate, eDate){     var divToBeWorkedOn = "#AjaxPlaceHolder";     var webMethod = "http://MyWebService/Web.asmx/GetInfoByDates";     var parameters = "{'sDate':'" + sDate + "','eDate':'" + eDate + "'}";      $.ajax({         type: "POST",         url: webMethod,         data: parameters,         contentType: "application/json; charset=utf-8",         dataType: "json",         success: function(msg) {             $(divToBeWorkedOn).html(msg.d);         },         error: function(e){             $(divToBeWorkedOn).html("Unavailable");         }     }); } 

I hope that helps.

Please note that this requires the 3.5 framework to expose JSON webmethods that can be consumed in this manner.

like image 52
Bobby Borszich Avatar answered Sep 16 '22 14:09

Bobby Borszich