简述get与post区别

get和post在HTTP中都代表着请求数据,其中get请求相对来说更简单、快速,效率高些。

get对于请求数据和静态资源(HTML页面和图片),在低版本浏览器下都会缓存。高版本浏览器只缓存静态资源,不缓存数据

post从服务器端推送数据(给的多,拿得少),而get是从服务器获取数据(给的少,拿得多)

post请求包含跟多的请求头,所以速度没有get快

post在真正接受数据之前会先将请求头发送给服务器进行确认,然后才真正发送数据

get请求方式

btn.onclick = function(){
    let xhr = new XMLHttpRequest;
    xhr.open('get','/get?ren='+encodeURI(txt.value),true);//
    // 其中url里面get是后端给的,?后面user是前后端定义的(或者后端提供)
    xhr.onload = function(){
        console.log(xhr.responseText);
    }
     xhr.send();
}

post请求方式

btn.onclick = function(){
    let xhr = new XMLHttpRequest;
    xhr.open('post','/post',true);
    //设置头信息
    xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    xhr.onload = function(){
        console.log(JSON.parse(xhr.responseText));
    }
     xhr.send('user'+txt.value);
}

两者区别

1、get比post速度块

2、get相对post安全性低

3、get有缓存,post没有

4、get的url参数可见,post不可见

5、get请求参数会保留历史记录,post中参数不会保留

6、get请求数据放在url,post数据在http包体(requrest body)内

7、get只接受ASCII字符的参数数据类型,post没有限制

8、get会被浏览器主动catch,post不会,需要手动设置

9、get在浏览器回退时无害,post会再次提交请求

10、get体积小(url字节长度每个浏览器不一样),post可以无限大(根据php.ini 配置文件设定)

原文地址:https://www.cnblogs.com/theblogs/p/10115214.html