1、POST请求
function submit(regForm) {
//创建对象
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader("content-type","application/json");
//发送请求
xhr.send(JSON.stringify(regForm));
xhr.onreadystatechange = function () {
if(xhr.readyState == 4) {
if (xhr.status == 200) {
// 返回对象
var msg = xhr.responseText;
msg = JSON.parse(msg);
console.log(msg)
}
}
};
}
2、GET请求
//创建对象
var xhr = new XMLHttpRequest();
//设置请求的类型及url
xhr.open('get', url);
xhr.setRequestHeader("content-type","application/json");
//发送请求
xhr.send();
xhr.onreadystatechange = function () {
if(xhr.readyState == 4) {
if (xhr.status == 200) {
// 返回对象
var msg = xhr.responseText;
msg = JSON.parse(msg);
}
}
};