您的位置:首页 > 理论基础 > 计算机网络

黑马程序员---XMLHttpRequest 对象open参数中使用post与get区别

2012-04-19 08:06 761 查看
----------------------
Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------                                        

 
POST:用"POST"方式发送数据,可以大到4MB

GET:用"GET"方式发送数据,只能256KB

如果请求带有参数的化实用POST方式,POST方式将参数放置在页面的隐藏控件内

没有参数使用GET方式

对于请求的页面在中途可能发生更改的,也最好用POST方式

用GET方式可能会拿不到最新的信息   (摘至网络)

 
xmlhttp.open("POST","GetDate1.ashx?id="+encodeURI("林其响"),false)
 xmlhttp.open("GET","GetDate1.ashx?id="+encodeURI("林其响")+"&ts"+new
Date(),false)
 
在使用post时,浏览器不会缓存所请求的内容,当使用get是默然浏览器就会缓存所请求的东西,当然这里可以可以使用一个技巧当使用get请求时浏览器不缓存,就是使没次的参数不一样,这样我们就可以使用
new Date() 每次请求时间都是不一样,这样就达到效果咯。比如在做汇率的时候,由于汇率是每天都有变化的,当时候get是就应该在参数中使用new
Date()。
 
下面这个例子就是使用post和get请求
Date1.htm文件:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD
XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>无标题页</title>
   <scripttype="text/javascript">
       function btnClick(){
           var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
           if(!xmlhttp){
               alert("创建xmlhttp对象异常");
               returnfalse
cf08
;
           }
           xmlhttp.open("POST","GetDate1.ashx?id="+encodeURI("温家宝"),false)
           //xmlhttp.open("GET","GetDate1.ashx?id="+encodeURI("温家宝")+"&ts"+new
Date(),false)
           
           xmlhttp.onreadystatechange=function(){
               if(xmlhttp.readyState==4){
                   if(xmlhttp.status==200){
                       document.getElementById("text1").value=xmlhttp.responseText;
                   }
                   else{
                       alert("Ajax服务器返回错误!");
                   }
               }
           }
           xmlhttp.send();
       }
   </script>
</head>
<body>
   <inputid="Text1"type="text"/><inputid="Button1"type="button"value="button" onclick="btnClick()"/>
</body>
</html>
 
GetDate1.ashx文件
 
<%@WebHandlerLanguage="C#"Class="GetDate1"%>
using System;
using System.Web;
publicclassGetDate1
:IHttpHandler {
       publicvoid ProcessRequest (HttpContext
context) {
       context.Response.ContentType ="text/plain";
      string
id=context.Request["id"];
       context.Response.Write(DateTime.Now.ToString()+"----"+id);
   }
    publicbool IsReusable {
       get {
           returnfalse;
       }
   }
}
 
 
 ----------------------
Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息