JQuery-AJAX calls the ASP.NET page method. How to return value in jQuery?

If I use jQuery AJAX to call a specific method on an ASP.NET page, how can I return that method back to the AJAX method that called it?

Update

My situation: 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 user interface 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 of Microsoft's ASP.NET AJAX. I agree with them.

+5
source share
3 answers

( ).

, MVC, , , ASP.Net, ( Aspx ). Ashx.

+1

ASP.NET( WCF ) (ASHX) JSON . JSON ( - ), - , json , javascript.

, , , , JSON , javascript .

:

<%@ 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;
        }
    }
}

:

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);
+1

All Articles