zoukankan      html  css  js  c++  java
  • js 实现ajax(get和post)

    get和post的区别:
    1.GET产生一个TCP数据包;POST产生两个TCP数据包。
      对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器响应200(返回数据);
      而对于POST,浏览器先发送header,服务器响应100 continue,浏览器再发送data,服务器响应200 ok(返回数据)。
    2.get请求数据有限制,post请求数据没有限制
    3.请求参数在url中发送,post请求参数在http消息主体中发送

        //get
          function get() {
              //创建XMLHttpRequest
            let xhr = new XMLHttpRequest();
              //监听响应
            xhr.onreadystatechange = function () {
              if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {
                console.log(xhr.responseText);
              }
            };
            xhr.open("GET","your url",true);
            xhr.send();
          }
    
          //post
          function post () {
              //请求参数、url、创建XMLHttpRequest
            let data = 'name=tom&age=18',
              url = 'your url',
              xhr = new XMLHttpRequest();
    
            xhr.open('post', url);
              //设置header
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xhr.send(data);
            xhr.onreadystatechange = function () {
              if (xhr.readyState === 4 && ( xhr.status === 200 || xhr.status === 304 )){
                console.log(xhr.responseText);
              }
            }
          }

    参考:1.https://blog.csdn.net/u012391923/article/details/53197387?utm_source=blogxgwz3
       2.http://www.runoob.com/tags/html-httpmethods.html

  • 相关阅读:
    太忙了
    Delphi 的接口(2) 第一个例子
    Delphi 的接口(3) 关于接口的释放
    VS.NET让我做了一场恶梦
    [推荐阅读]The Best Of .The NET 1.x Years
    向大家说声对不起
    [致歉]16:30~17:10电信网络出现问题
    服务器恢复正常
    [SharePoint]更改活动目录(AD)中用户名的问题
    [正式决定]博客园开始接受捐助
  • 原文地址:https://www.cnblogs.com/bear-blogs/p/9825904.html
Copyright © 2011-2022 走看看