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
基础教程推荐
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 将数据集转换为列表 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
