application/x-www-form-urlencoded的demo,可以在浏览器的console里运行
var data = "aaa=123&bbb=123";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "/");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
我们在baidu里运行结果
multipart/form-data一般用来上传文件,当然也可以不上传文件
var data = new FormData();
data.append("aaa", "123");
data.append("bbb", "123");
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "/");
xhr.send(data);
可以看到formdata的真实面目.以及有分隔符