前台代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.6.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btn1").click(
function () {
alert("123");
$.get("ggg.aspx?id=1&Math.random()", function (data) { $("#re1").text(data) });
});
$("#btn2").click(
function () {
$.get("ggg.aspx?id=1&name=name&Math.random()", function (data) { alert("456"); $("#btn2").val(data) });
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= GetTable() %> <br/>方法
</div>
<div id="re1">
<input type="button" id="btn1" name="1" value="btn1" />div1
</div>
<div id="re2">
<input type="button" id="btn2" name="2" value="btn2" />div2
</div>
</form>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ggg : System.Web.UI.Page
{
int n = 0;
protected void Page_Load(object sender, EventArgs e)
{
string id= Request.QueryString["id"];
string name = Request.QueryString["name"];
if (id!=null&&name==null)
{
Response.Write("send1");
Response.End();
}
if (id!=null&&name!=null)
{
Response.Write("send3");
Response.End();
}
}
protected string GetTable()
{
n++;
return n.ToString ()+"gettable()";
}
}
运行后输出三种网页
第一种网页
btn1单击 页面先弹出 对话框123 然后 id是 re1的div中的内容为send1 btn1清除
btn2单击 页面先弹出 对话框456 然后 id是 re2的div中的btn2的value 显示 send3
第二种网页
btn1单击 页面返回 send1
第三种网页
直接报错不能运行
花了一下午的时间。
有人重写了vs。
ashx一般处理程序可以通过判断参数最后输出Response。End();来处理多个其他页面发送的ajax请求。
整理思路 ajax 交互式网页 xmlhttprequest对象
1.发送ajax请求 页面不刷新 报文监视 发送了请求 监视报文中的content 回发的json格式的字符串 异步刷新。
2. 整个异步请求对象是javascript对象 ,运行在客户端,建html就行。get请求浏览器缓存,请求没有发送到服务器。
3.ajax同步请求和异步请求的区别xhr.open("get","1.aspx?id=1",true) true异步请求 false 同步请求
true异步不等请求的数据返回就向下执行 false 同步,请求的数据返回处理后,向下执行。
4.请求的路径 项目名称网页名称最好不是中文 中文url编码 encodeURL("整个请求对象处理其中的参数") encodeURLComponent("请求的参数")
5.xhr是发送请求的对象,不是浏览器,F5刷新的时候,重新执行上次浏览器的请求,所以无刷新请求没有执行,重新请求的整个页面。
6.一个小实验在aspx页面中
protected void Page_Load(object sender, EventArgs e)
{
string id=Request.QueryString["id"];
if (id=="1")
{
Response.Write("1");
Response.End();
}
else
{
Response.Write("3");
}
Response.End();
}
http://localhost:4398/1.aspx?&id=1&0.8486364427953959
第一次输出this request has no response data available
第二次输出 1
7 在aspx中一旦context.Response.End();后面的代码不执行,输出缓存发送到浏览器。最终正确的网页是输出缓存的内容,前台的html代码也不输出到浏览器。