Ajax方法1:
前臺aspxjs代碼:
<script language="javascript" type="text/javascript">
function CallServer()
{
var a = document.getElementById("text").value;
<%=ClientScript.GetCallbackEventReference(this,"a","ReceiveServer","null") %>//回調方法
}
//返回結果
function ReceiveServer(result)
{
document.getElementById("text").value = result;
}
</script>
後臺cs代碼:
#region 回調(一定要用這兩個方法 而且名字還是RaiseCallbackEvent跟GetCallbackResult)
private stringResult;
//用來獲取前臺返回的值
public voidRaiseCallbackEvent(string eventArgument)
{
Result = eventArgument;
}
//用來返回給前臺的值
public stringGetCallbackResult()
{
Console.WriteLine("處理前Result" +Result);
int i = int.Parse(Result);
i++;
Result = i.ToString();
Console.WriteLine("處理後Result" +Result);
return Result + " " + DateTime.Now.ToString();
}
#endregion
Ajax方法2:
前臺aspx代碼:
<script src="jQuery.js" type="text/javascript"></script>
<script src="jquery-1.7.1.js"></script>
<script type="text/javascript">
function testGet() {
$.ajax({
type: post, //方式post get
url: 'NormalPage.aspx', //指向的URL
async: true,
success: function (result) {
alert(result); //處理後臺的返回值
},
error: function (){
alert('ERROR!');
}
});
}
</script>
後臺NormalPage.aspx的代碼:
在PageLoad裏面:
protected voidPage_Load(object sender, EventArgs e)
{
string action = Request.QueryString["action"];
Response.Clear(); //清除所有之前生成的Response內容
Response.Write(action + " " +DateTime.Now.ToString());
Response.End(); //停止Response後續寫入動作,保證Response內只有我們寫入內容
}
Ajax方法3:
function ajax()
{
var xmlhttp = window.XMLHttpRequest ? newXMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); //創建XMLHTTP對象,考慮兼容性
xmlhttp.open("POST", "Handler1.ashx?"+ "i=" + document.getElementById("txt1").value,true); //“準備”向服務器的AJAXTest.ashx發出Post請求(GET可能會有緩存問題)。這裏還沒有發出請求
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4)//readyState == 4 表示服務器返回完成數據了。之前可能會經歷2(請求已發送,正在處理中)、3(響應中已有部分數據可用了,但是服務器還沒有完成響應的生成)
{
if (xmlhttp.status == 200)//如果狀態碼爲200則是成功
{
document.getElementById("txt1").value= xmlhttp.responseText;
}
else
{
alert("AJAX服務器返回錯誤!");
}
}
}
//不要以爲if (xmlhttp.readyState == 4) {在send之前執行!!!!
xmlhttp.send(); //這時纔開始發送請求
//發出請求後不等服務器返回數據,就繼續向下執行,所以不會阻塞,界面就不卡了,這就是AJAX中“A”的含義“異步”。試着在ashx加一句//Thread.Sleep(3000);
}
//下面寫兩個input
//<input type="button" value ="發送" onclick="ajax()"></input>
//<input type="text" id ="txt1" value ="1"/>
後臺Handler1.ashx代碼:
在ProcessRequest裏面 如果是aspx文件,就寫在Page_Load裏面
public voidProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/HTML";
string Request_a = context.Request["i"];
int Request_i = Convert.ToInt32(Request_a);
Request_i++;
context.Response.Write(Request_i.ToString());
}
Ajax方法4:
前臺test.aspx代碼:
<script type="text/javascript">
functionajax()
{
var a =document.getElementById("te1").value;
var b =WebForm3.ChangeValue(a).value;
document.getElementById("te1").value = b;
}
</script>
//下面有兩個input
//<input type="button" id ="bt1" value="發送" onclick="ajax()" />
//<input type="text" id ="te1" value ="1"/>
後臺test.cs代碼:
首先引用Ajax.dll
usingAjax;
在PageLoad裏面
protected void Page_Load(object sender, EventArgs e)
{ //命名空間 類名
Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication4.WebForm3));
}
[Ajax.AjaxMethod()]
public stringChangeValue(string res)
{
int i =Convert.ToInt32(res);
i++;
returni.ToString();
}
在web.config裏面
在<system.webServer>裏面
添加:
<handlers>
<addname="ajax"verb="POST,GET"path="ajax/*.ashx"type="Ajax.PageHandlerFactory, Ajax" />
</handlers>