Its interesting and easy to implement. As always- lets go directly to an example and see how to implement it...
In this example we will create a webmethod which will return a message to client side
1) Create an ASP.NET Application.
2) Add a new page 'WebForm1.aspx' and make it the start up page.
3) In WebForm1.aspx include jQuery file as given below. If you do not have jQuery file, you can download the latest files from http://jquery.com/
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
4) In code behind page (WebForm1.aspx.cs) create a webmethod to return some data as given below. Make sure to add the attribute [WebMethod()] to the function. You have to include the namespace System.Web.Services. (using System.Web.Services;)
[WebMethod()] public static string GetData(int userid) { /*You can do database operations here if required*/ return "my userid is" + userid.ToString(); }
5) Add script tags and include the function to call the web method. Pass the parameter (in this case 'userid') to web method as JSON object
function asyncServerCall(userid) { jQuery.ajax({ url: 'WebForm1.aspx/GetData', type: "POST", data: "{'userid':" + userid + "}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); } }); }
6) Add button on aspx page and call the ajax function on click event.
<input type="button" value="click me" onclick="asyncServerCall(1);" />
7) DONE! Run the app and click the button, you can see that the webmethod is called and data is returned.
Wasn't that easy? Let me know if you face any issue in running this example. I used Visual Studio 2010 to run this app.
13 comments:
Good stuff man, thanks a lot.
THANKS Dude!
Nice Work Friend !
How to Bind RADGridview in jQuery ajax ?
Thank You it works smoothly.
Thanks :) Its working..
sir it working fine thank a lots........
:)
thanks
the ajax call returns full html rather than the result of the method C#.
Thanks a lot man!!!
Great Work on ASP.NET AJAX
Thanks alot
Great Work on ASP.NET AJAX
Thanks Alot
Ya that's great to have blog like this it may clear the many thought from this
Pega Training
PlSQL Training
AWS Training
Teradata Training
Qlikview Training
Informatica MDM Training
Post a Comment