Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery-AJAX calling ASP.NET page method. How to return value back to jQuery?

Tags:

jquery

asp.net

If I use jQuery AJAX to call a specific ASP.NET page method how to have that method return a value back to the AJAX method that called it?

Update

My situation is I have an existing web application with many existing methods. I would like to be able to use jQuery to execute some of these methods and then update the UI with the results. My mandate is to stay away from ASP.NET AJAX and stick with jQuery. Management is concerned about continued development and support with ASP.NET AJAX from Microsoft. I agree with them.

like image 756
webworm Avatar asked Jan 31 '11 18:01

webworm


2 Answers

You can use JQuery with page methods this way: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

The success callback contains a parameter with the returning data.

HTH.

like image 78
Brian Mains Avatar answered Sep 19 '22 23:09

Brian Mains


There are two ways to skin this cat (that I am familiar with).

  1. The ".Net Way" which involves a Web Method and a Script Manager (see here: http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx).

  2. The "Old Skool Way" which involves simply writing a response out by determining what was called. Typically you'd use a framework like MVC so going to http://www.MyWebsite.com/Object/Method/Id can map back to Object.Method(id).

You can do this without a framework like MVC but it makes things a little more difficult and, if you go that route, you should really use an ASP.Net handler rather than an actual page (since you don't need the Aspx overhead). This is an Ashx file.

like image 31
Kris Avatar answered Sep 22 '22 23:09

Kris