前台代碼
<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代碼也不輸出到瀏覽器。
