AJAX03-发送POST请求

发送请求时带有参数常用post请求

  • open:xmlHttp.open(“POST”...);

  • 设置请求头: xmlHttp.setRequestHeader(“Content=Type”,”application/x-www-form-urlencoded”);

  • send:xmlHttp.send(“username=zhangsan&age=18”);

Servelt .java

public class Servelt extends HttpServelt {
      public void doGet(HttpServeltRequest request,HttpServeltResponse response)  throws Exception{
          System.out.println("Hellow World!");
          response.getWrtter().print("hellow ajax!!!");
      }
      public void doPOST(HttpServeltRequest request,HttpServeltResponse response)  throws Exception{
          //防止乱码************************
          response.setContentType("text/html;charest=utf-8");
          response.setCharacterEncoding("UTF-8");
          String userName=request.getParameter("username");//获取请求参数
          System.out.println("POST:Hellow World!"+userName);
          response.getWrtter().print("POST:hellow ajax!!!"+userName);
      }
}

Ajax.jsp

<head>
<script type="text/javascript">
   //创建异步对象
   function createXMLHttpRequest(){
    try {
        return new XMLHttpRequest();
    }catch(){
       try {
           return new ActiveXobject("Msxml2.XMLHTTP");
       }catch(e){
          try{
              return new ActiveXobject("Microsoft.XMLHTTP");
          }catch(e){
             alert("用的啥浏览器???");
             throw e;
          }
       }
     }
  }
   window.onland =function (){ //文档加载完毕之后执行
   var bta =document.getElenmentById("bta");
       bta.onclick =function(){//给按钮的点击事件注册监听
       //第一步:得到异步对象
       var xmlHttp = createXMLHttpRequest();
       //第二步:打开与服务器连接,3个参数:请求方式、url、异步请求
       xmlHttp.open("POST","<c:url value="/Servelt"/>",true);
       //***********************************************
       //修改open方法,设置请求头
       xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
       //第三步:发送请求
       xmlHttp.send("username=zhangsan&password=123");//get请求没有请求体,但也要给出null
       //第四步:设置监听
       xmlHttp.onreadystatechange =function (){
            if(xmlHttp.readyState ==4 && xmlHttp.status =200){
            //获取服务器的响应结束
            var text = xmlHttp.responseText();
            //获取h1元素
            var h1 =document.getElenmentByID("h1");
            h1.innerHtml =text;
            }
          }
       };
   };
 </script>
 </head>
 <body>
   <button id="bta">点击这里</button>
   <h1 id="h1"></h1>
 </body>

POST与get区别

使用上的区别:

  • 参数:GET把参数包含在URL中,POST通过request body传递参数;
  • 参数:get会保留在浏览器里面;
  • 权限:http提供4种方式 put\delete\post\get,get为查询,post为修改;
  • 安全性:从权限来说get相对安全,但是有些敏感信息不能放在url里面;
  • 编码:Get只能进行url编码,post更多;
  • 速度:get把header和data一次全部发送出去,响应200;post先发送header,响应100再发送data,响应200;

本质区别:

  • GET和POST本质上就是TCP链接,并无差别。
  • 但是由于HTTP的规定和浏览器/服务器的限制,导致他们在应用过程中体现出一些不同。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值