分析
- 使用原生AJAX的POST请求,如果不指定请求头Request Header
默认使用的Content-Type是text/plain;charset=UTF-8或者application/json - 请求的Content-Type设置为application/x-www-form-urlencoded
那么,这个Post请求被认为是HTTP POST表单请求,
参数形式是key=value&key1=value1这种形式
代码
//发送异步请求
function sendAjaxRequest() {
//这里使用Ajax局部提交form表单
//使用原生AJAX的POST请求,
//如果不指定请求头Request Header,
//默认使用的Content-Type是text/plain;charset=UTF-8,或者application/json
const xhr = new XMLHttpRequest();
//open(请求方式, 请求路径, 是否异步);
xhr.open('POST', '/day15/maliceRegister', true);
//请求的Content-Type设置为application/x-www-form-urlencoded,
//那么,这个Post请求被认为是HTTP POST表单请求,
//参数形式是key=value&key1=value1这种形式
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//send(string):string仅用于 POST 请求
//string需要是name=xxx&pwd=xxx格式
xhr.send("username=z&password=123");
}