Post&Send
发送请求
GET
function senMsg() {
//1.创建一个XMLHttpRequest对象
var xhr = new XMLHttpRequest()
//2.调用open方法打开链接
//open有三个参数
//1.请求的method
//2.请求的url
//3.是否异步,默认值为true,一般这个参数可以不传
xhr.open('get',url)
//3.发送请求
xhr.send()
//4.监听状态的改变
xhr.onreadystatechange = function() {
//判断状态,0~4五种状态,4代表最终的状态
if(xhr.readyState === 4){
//判断状态
if(xhr.status === 200){
console.log(xhr.responsText);
}
}
}
}
POST
function sendMsg() {
var xhr = new XMLHttpRequest()
//这里的method传post
xhr.open('post','url')
//设置请求头的content-type
xhr.setRequestHeader('content-Type','application/x-www-form-urlencoded');
//传递参数
xhr.send('name=zhangshan&age=18')
xhr.onreadystatechange = function() {