可以使用ASP.NET Web API或Web服务向Web表单发送数据。以下是一个使用ASP.NET Web API向Web表单发送JSON数据的示例:
在Web API控制器中,使用JsonResult方法返回JSON数据:
[HttpGet]
public JsonResult Get()
{
var data = new { Name = "John Doe", Age = 30 };
return Json(data, JsonRequestBehavior.AllowGet);
}
在Web表单中,使用jQuery的ajax方法从Web API中获取JSON数据:
$.ajax({
type: "GET",
url: "/api/values",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
$("#name").text(data.Name);
$("#age").text(data.Age);
},
error: function(error) {
console.log(error);
}
});
在Web表单中,将JSON数据显示在HTML标记中:
Name:
Age: