jQuery-AJAX 调用 ASP.NET 页面方法。如何将值返回给 jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4854834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:03:06  来源:igfitidea点击:

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

asp.netjquery

提问by webworm

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?

如果我使用 jQuery AJAX 调用特定的 ASP.NET 页面方法,如何让该方法将值返回给调用它的 AJAX 方法?

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.

我的情况是我有一个现有的 Web 应用程序,其中包含许多现有方法。我希望能够使用 jQuery 来执行其中一些方法,然后使用结果更新 UI。我的任务是远离 ASP.NET AJAX 并坚持使用 jQuery。管理层关注 Microsoft 的 ASP.NET AJAX 的持续开发和支持。我同意他们的观点。

回答by Brian Mains

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

您可以通过这种方式使用 JQuery 和页面方法: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.

哈。

回答by Kris

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/Idcan map back to Object.Method(id).

  1. 涉及 Web 方法和脚本管理器的“.Net 方式”(参见此处:http: //geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods -in-aspx-pages.aspx)。

  2. “Old Skool 方式”包括通过确定调用的内容来简单地写出响应。通常,您会使用像 MVC 这样的框架,因此访问http://www.MyWebsite.com/Object/Method/Id可以映射回 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.

你可以在没有像 MVC 这样的框架的情况下做到这一点,但它会让事情变得更加困难,如果你走那条路,你真的应该使用 ASP.Net 处理程序而不是实际页面(因为你不需要 Aspx 开销) . 这是一个 Ashx 文件。

回答by Mike Marshall

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 (hereis 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.

使用纯 ASP.NET(这里不说 WCF)我会使用处理程序 (ASHX) 文件,并使用 JSON 作为交换格式。我不会深入研究 JSON 的细节(这里是一个不错的开始),但这个想法是服务器上的一个轻量级处理程序,它生成 json 文本并将其返回给客户端,然后客户端可以轻松地在 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.

这显然是一个简化的例子,但要点是 JSON 可以是从服务器驱动的数据,并且很容易被客户端上的 javascript 使用。

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