要求:当前WebForm1页面发送请求,请求WebForm2页面,WebForm2页面返回数据,通过lable显示出来。
WebForm1页面JQuery代码
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="按钮" id="btn" onclick="get()" />
<asp:Label ID="Label1" runat="server" ></asp:Label>
</div>
</form>
</body>
</html>
<script>
function get() {
$.ajax({
url: "WebForm2.aspx", //请求的url地址
//dataType: "json", //返回格式为json
async: true,//请求是否异步,默认为异步,这也是ajax重要特性
//data: { "id": "1", "name": "名字" }, //参数值
type: "GET", //请求方式
beforeSend: function () {
//请求前的处理
},
success: function (req) {
$("#Label1").text(req);
},
complete: function () {
//请求完成的处理
},
error: function () {
//请求出错处理
}
});
}
</script>
WebForm2后台代码 (主要通过Response.Write方法返回数据, 也可以返回html,xml,json,text等格式,在这拼就完事了)
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Write("请求后的数据");
Response.End();
}
点击按钮后的页面: