向 .aspx 页面发送 AJAX 请求并返回 JSON [英] Send AJAX request to .aspx page and return JSON

查看:30
本文介绍了向 .aspx 页面发送 AJAX 请求并返回 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以向 .asmx 页面发送 AJAX 请求.而且我还知道 .asmx 页面通过网络方法处理 AJAX 请求.

I know that it is possible to send an AJAX request to an .asmx page. And I also know that an .asmx page handles an AJAX request via a web method.

是否也可以向 .aspx 页面发送 AJAX 请求?如果是这样,.aspx 页面是否也通过 Web 方法处理 AJAX 请求?请注意,我想从 .aspx 页面返回 JSON 响应.这可能吗?

Is it also possible to send an AJAX request to an .aspx page? If so, does an .aspx page also handle an AJAX request via a web method? Note that I would like to return a JSON response from the .aspx page. Is this possible?

推荐答案

您可以在 .aspx 页面的代码隐藏中定义 Web 方法,然后调用它们:

You can define web methods in the code-behind of your .aspx page and then call them:

[WebMethod]
public static string doSomething(int id)
{
    ...
    return "hello";
}

然后,在您的 jQuery 代码中调用 Web 方法:

And then, to call a web method in your jQuery code:

$.ajax({
    type: "POST",
    url: "YourPage.aspx/doSomething",
    data: "{'id':'1'}",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        var returnedstring = data.d;
        var jsondata = $.parseJSON(data.d);//if you want your data in json
    }
});

这里链接以开始使用.

这篇关于向 .aspx 页面发送 AJAX 请求并返回 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆