//js原生的ajax请求
//使用xhr发送GET请求
// 1、创建XHR对象
var xhr = new XMLHttpRequest();
// 2、监听XHR对象的状态改变
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
//处理相应请求的消息 xhr.responseText
}else{
//响应完成但存在问题
}
}
}
// 3
xhr.open('GET','url地址',true);
// 4
xhr.send(null);
//js原生的ajax请求
//使用xhr发送post请求
// 1、创建XHR对象
var xhr = new XMLHttpRequest();
// 2、监听XHR对象的状态改变
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
//处理相应请求的消息 xhr.responseText
}else{
//响应完成但存在问题
}
}
}
// 3
xhr.open('POST','url地址',true);
// 3.5
xhr.setRequestHeader('Content-Type','application/x-www-dorm-urlencoded')
// 4
xhr.send('需要发送的值');
欢迎关注我的个人技术公众号!javascript艺术