浏览器如何使用POST请求?详解GET和POST区别
浏览器可以使用'GET'和'POST'两种请求方式。
如果需要将请求方式从'GET'改为'POST',可以在发送请求时将请求方法设置为'POST',例如:
var xhr = new XMLHttpRequest();
xhr.open('POST', 'url', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('data');
其中,xhr.open('POST', 'url', true)将请求方法设置为'POST';xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')设置请求头;xhr.send('data')发送请求体。
需要注意的是,如果是通过表单提交数据,可以直接将form的method属性设置为'POST'即可。
原文地址: https://www.cveoy.top/t/topic/orV1 著作权归作者所有。请勿转载和采集!