ajax的基本创建
let xhr = new XMLHttpRequery();
xhr.open(type,网址)
xhr.send()
xhr.onload = function(){
console.log(xhr.response)
}
ajax的请求方式
get请求
let xhr = new XMLHttpRequest()
xhr.open('get','http://localhost:8888/test/first')
xhr.send();
xhr.onload = function(){
console.log(xhr.response)
}
let xhr = new XMLHttpRequest()
xhr.open('get','http://localhost:8888/test/second')
xhr.send()
xhr.onload = function(){
console.log(xhr.response)
let res = JSON.parse(xhr.response)
let {message,code} = JSON.parse(xhr.response)
console.log(message)
}
let xhr = new XMLHttpRequest();
xhr.open('get','http://localhost:8888/test/third?name=zs&age=15');
xhr.send();
xhr.onload = function(){
console.log(xhr.response)
}
post请求
let xhr = new XMLHttpRequest();
xhr.open('post','http://localhost:8888/test/fourth');
xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
let res = 'name=zs&age=25';
xhr.send(res);
xhr.onload = function(){
console.log(xhr.response)
}