Send AJAX request to .aspx page and return JSON(向 .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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向 .aspx 页面发送 AJAX 请求并返回 JSON
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
