5

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.

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

With pure ASP.NET (not talking WCF here) I'd go with a handler (ASHX) file, and use JSON as the interchange format. I won't get into the details of JSON (here is a decent start), but the idea is a lightweight handler on the server that generates json text and returns it to the client, which can then use the structure easily in javascript.

This is obviously a simplified example but the gist is the JSON can be data driven from the server and easily consumed by the javascript on the client.

server:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/json";
        context.Response.WriteFile("~/myData.json");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

client:

myData = 
      (function () 
       {
          var json = null;
          $.ajax({
              'async': false,
              'global': false,
              'url': "handler.ashx",
              'dataType': "json",
              'success': function (data) {    
                  // this code is called when the 
                  // data is returned from the server              
                  json = data;
              }
          });
          return json;    
      }
          )(); 

alert(myData.MyArray[0].MyProperty);

6 Comments

I am not familiar with "Handler" types. Couldn't the AJAX method just execute the standard web method and get results from it?
An ASP.Net handler is just like an ASPX page except that it has much lower overhead and is designed for things like this (think RSS feeds, too). You can use jQuery to execute a WebMethod (one of the suggestions above). mjmarsh's approach is much more light weight and should be a bit faster as it has less overhead than dealing with not only an ASPX file but also the .Net event structure.
@Kris: That's not correct. When you request a "page method" defined in an ASPX page, you don't touch any of the traditional WebForms page life cycle at all. In fact, they even have to be static methods, because they're executed without an instance of the Page being created. It's a sort of special case shorthand for declaring an ASMX "ScriptService" method inline in an ASPX page.
I would not use a custom handler for something, a user can put in. Your custom handler may not be implementing no of the security measures to prevent some known hacks. Use a Webservice.
@Dave, um, I never said you touch ANY of the page life cycle. Using a web method over a technique like this DOES have more overhead. Explore the data sent and received to verify for yourself.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.