国产精品毛片一区二区,欧美熟妇brazzers,丰满女邻居的嫩苞张开视频,天天爽夜夜爽夜夜爽

南京北大青鳥

全國咨詢電話:15195455103

三分鐘了解北大青鳥
當前位置:南京北大青鳥 > 新聞動態(tài) > 焦點資訊

ajax請求的五個步驟(ajax怎么實現(xiàn)前后端交互)

來源:Web前端資訊? ? ? 作者:南京北大青鳥 ? ??

ajax請求的五個步驟

  ajax請求的五個步驟

  第一步,創(chuàng)建XMLHttpRequest對象

  第二步,注冊回調(diào)函數(shù)

  第三步,配置請求信息,open(),get

  第四步,發(fā)送請求,post請求下,要傳遞的參數(shù)放這

  第五步,創(chuàng)建回調(diào)函數(shù)

//第一步,創(chuàng)建XMLHttpRequest對象
var xmlHttp = new XMLHttpRequest();
function CommentAll() {
 //第二步,注冊回調(diào)函數(shù)
 xmlHttp.onreadystatechange =callback1;
 //{
 // if (xmlHttp.readyState == 4)
 // if (xmlHttp.status == 200) {
 // var responseText = xmlHttp.responseText;
 // }
 //}
 //第三步,配置請求信息,open(),get
 //get請求下參數(shù)加在url后,.ashx?methodName = GetAllComment&str1=str1&str2=str2
 xmlHttp.open("post", "/ashx/myzhuye/Detail.ashx?methodName=GetAllComment", true);
 //post請求下需要配置請求頭信息
 //xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 //第四步,發(fā)送請求,post請求下,要傳遞的參數(shù)放這
 xmlHttp.send("methodName = GetAllComment&str1=str1&str2=str2");//"
}
//第五步,創(chuàng)建回調(diào)函數(shù)
function callback1() {
 if (xmlHttp.readyState == 4)
 if (xmlHttp.status == 200) {
 //取得返回的數(shù)據(jù)
 var data = xmlHttp.responseText;
 //json字符串轉(zhuǎn)為json格式
 data = eval(data);
 $.each(data,
 function(i, v) {
 alert(v);
 }); 
 }
}

 

  后臺方法

private void GetAllComment(HttpContext context)
 {
 //Params可以取得get與post方式傳遞過來的值。
 string methodName = context.Request.Params["methodName"];
 //QueryString只能取得get方式傳遞過來的值。
 string str1 = context.Request.Form["str1"];
 //取得httpRequest傳來的值,包括get與post方式
 string str2 = context.Request["str2"];
 List<string> comments = new List<string>();
 comments.Add(methodName);
 comments.Add(str1);
 comments.Add(str2);
 //ajax接受的是json類型,需要把返回的數(shù)據(jù)轉(zhuǎn)給json格式
 string commentsJson = new JavaScriptSerializer().Serialize(comments);
 context.Response.Write(commentsJson);
 }

分享到:

相關(guān)閱讀:

近期文章

搶試聽名額

名額僅剩66名

教育改變生活

WE CHANGE LIVES