一、原生Ajax
![image-20230414220216576](https://i-blog.csdnimg.cn/blog_migrate/817c5679c8f08c86708a28a825e75b7a.png)
![image-20230414220238724](https://i-blog.csdnimg.cn/blog_migrate/f02a380664e68d23d10bf4401238207f.png)
<!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>Document</title>
</head>
<body>
<input type="button" value="获取数据" onclick="getData()">
<div id="div1"></div>
</body>
<script>
var xmlhttprequest = new XMLHttpRequest();
xmlhttprequest.open("GET","http://yapi.smart-xwork.cn/mock/169327/emp/list");
xmlhttprequest.send();
xmlhttprequest.onreadystatechange = function () {
if(xmlhttprequest.readyState==4 && xmlhttprequest.status==200){
document.getElementById("div1").innerHTML = xmlhttprequest.responseText;
}
}
</script>
</html>
![image-20230414220452638](https://i-blog.csdnimg.cn/blog_migrate/9bfa6a547705e069a8bbdf1be38422fa.png)
![image-20230414224338655](https://i-blog.csdnimg.cn/blog_migrate/6665a17c2510d34acdcdaf2c79e866e8.png)
二、Axios
![image-20230414224750589](https://i-blog.csdnimg.cn/blog_migrate/fedfd631c4c6d759ee6a8df9b7966f50.png)
<!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>Document</title>
<script src="js/axios-0.18.0.js"></script>
</head>
<body>
<input type="button" value="get" onclick="getMethod()">
<input type="button" value="post" onclick="postMethod()">
</body>
<script>
function getMethod() {
axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then((result)=>{
console.log(result.data);
})
}
function postMethod(){
axios.post("http://yapi.smart-xwork.cn/mock/169327/emp/deleteById","id=1").then((result)=>{
console.log(result.data);
})
}
</script>
</html>
![image-20230414231154562](https://i-blog.csdnimg.cn/blog_migrate/77a070e1c2af76110bc3fc0d4ebd5f12.png)
![image-20230414231220171](https://i-blog.csdnimg.cn/blog_migrate/a02da042d9b891dc025f5215d2463f31.png)
三、Axios案例
![image-20230414231258320](https://i-blog.csdnimg.cn/blog_migrate/c23ee04ed2d26afd42122e87a16e41c2.png)
<!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>axios案例</title>
<script src="js/vue.js"></script>
<script src="js/axios-0.18.0.js"></script>
<style>
#app{
max-width: 1680px;
width:80%;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="app">
<table border="1" cellspacing="0" style="margin: auto;">
<tr>
<th>编号</th>
<th>姓名</th>
<th>图像</th>
<th>性别</th>
<th>职位</th>
<th>入职日期</th>
<th>最后操作时间</th>
</tr>
<tr v-for="(user,index) in userall">
<td>{{index+1}}</td>
<td>{{user.name}}</td>
<td><img v-bind:src="user.image" width="100px"></td>
<td v-if="user.gender==1">男</td>
<td v-if="user.gender==2">女</td>
<td>{{user.job}}</td>
<td>{{user.entrydate}}</td>
<td>{{user.updatetime}}</td>
</tr>
</table>
</div>
</body>
<script>
new Vue({
el: "#app",
data:{
userall:[]
},
methods:{
},
mounted(){
axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then((result)=>{
this.userall=result.data.data;
});
}
})
</script>
</html>
![image-20230414235131885](https://i-blog.csdnimg.cn/blog_migrate/727ff0a43f7de63a861e6f7d70c0f4aa.png)