发布时间:2025-12-10 23:06:58 浏览次数:1
ajax请求的五个步骤
第一步,创建XMLHttpRequest对象
第二步,注册回调函数
第三步,配置请求信息,open(),get
第四步,发送请求,post请求下,要传递的参数放这
第五步,创建回调函数
//第一步,创建XMLHttpRequest对象varxmlHttp=newXMLHttpRequest();functionCommentAll(){//第二步,注册回调函数xmlHttp.onreadystatechange=callback1;//{//if(xmlHttp.readyState==4)//if(xmlHttp.status==200){//varresponseText=xmlHttp.responseText;//}//}//第三步,配置请求信息,open(),get//get请求下参数加在url后,.ashx?methodName=GetAllComment&str1=str1&str2=str2xmlHttp.open("post","/ashx/myzhuye/Detail.ashx?methodName=GetAllComment",true);//post请求下需要配置请求头信息//xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//第四步,发送请求,post请求下,要传递的参数放这xmlHttp.send("methodName=GetAllComment&str1=str1&str2=str2");//"}//第五步,创建回调函数functioncallback1(){if(xmlHttp.readyState==4)if(xmlHttp.status==200){//取得返回的数据vardata=xmlHttp.responseText;//json字符串转为json格式data=eval(data);$.each(data,function(i,v){alert(v);});}}后台方法
privatevoidGetAllComment(HttpContextcontext){//Params可以取得get与post方式传递过来的值。stringmethodName=context.Request.Params["methodName"];//QueryString只能取得get方式传递过来的值。stringstr1=context.Request.Form["str1"];//取得httpRequest传来的值,包括get与post方式stringstr2=context.Request["str2"];List<string>comments=newList<string>();comments.Add(methodName);comments.Add(str1);comments.Add(str2);//ajax接受的是json类型,需要把返回的数据转给json格式stringcommentsJson=newJavaScriptSerializer().Serialize(comments);context.Response.Write(commentsJson);}看完上述内容,你们掌握ajax请求的步骤是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注本站行业资讯频道,感谢各位的阅读!