24 尚硅谷 AJAX Axios函数发送AJAX请求
修改index.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jquery 发送 ajax 请求</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<button>GET</button>
<button>POST</button>
<button>AJAX</button>
<SCript>
const btns=document.querySelectorAll("button")
axios.defaults.baseURL='http://192.168.1.254:8000'
btns[0].onclick=function(){
axios.get('/axios-server',{
params:{
id:100,
vip:7
},
headers:{
name:'atguigu',
age:20
}
}).then(value=>{
console.log(value);
})
}
btns[1].onclick=function(){
axios.post('/axios-server',{
username:'admin',
password:'admin'
},{
params:{
id:100,
vip:7
},
headers:{
height:180,
weight:180
}
}).then(value=>{
console.log(value);
})
}
btns[2].onclick=function(){
axios({
method:'POST',
url:'/axios-server',
params:{
vip:10,
level:30
},
headers:{
a:100,
b:200
},
data:{
username:'admin',
password:'admin'
}
}).then(response=>{
console.log(response);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.data)
})
}
</SCript>
</body>
</html>