关于ajax中get请求与post请求的区别可以从传参方式,请求头,参数值类型等方面分析。
post请求:
//请求地址
var url = '01.php';
//open参数为post
xhr.open('post',url);
//设置请求头
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//设置post请求参数值
xhr.send('id='+data);
get请求:
xhr.open('get','01.php?'id='+data);
xhr.send();
1.传参方式:
get请求在url尾部传递参数,post请求在send()方法中传递参数;
2.请求头:
post请求头需要声明请求头xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');而get请求不需要设置。
3.参数值类型:
get传参值只能是数字、字符串。post除了可以传递数字、字符串以外,还可以传递二进制数据。