> 文章列表 > 23-Ajax-axios

23-Ajax-axios

23-Ajax-axios

一、原生Ajax

23-Ajax-axios

23-Ajax-axios

<!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>//Ajax 的核心是 XMLHttpRequest 对象// 1、创建XMLHttpRequest对象var xmlhttprequest = new XMLHttpRequest();// 2、发送异步请求xmlhttprequest.open("GET","http://yapi.smart-xwork.cn/mock/169327/emp/list");xmlhttprequest.send();// 3、获取服务器响应数据xmlhttprequest.onreadystatechange = function () {if(xmlhttprequest.readyState==4 && xmlhttprequest.status==200){document.getElementById("div1").innerHTML = xmlhttprequest.responseText;}}
</script>
</html>

23-Ajax-axios

23-Ajax-axios

二、Axios

23-Ajax-axios

<!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({method:"get",url:"http://yapi.smart-xwork.cn/mock/169327/emp/list"}).then((result)=>{//成功回调函数console.log(result.data);}) */// 更加简便的方法:axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then((result)=>{console.log(result.data);})}function postMethod(){
/*         axios({method:"post",url:"http://yapi.smart-xwork.cn/mock/169327/emp/deleteById",data:"id=1"}).then((result)=>{console.log(result.data);}) */axios.post("http://yapi.smart-xwork.cn/mock/169327/emp/deleteById","id=1").then((result)=>{console.log(result.data);})}
</script>
</html>

23-Ajax-axios

23-Ajax-axios

三、Axios案例

23-Ajax-axios

<!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><!-- 这里比较容易出错,图片必须用img标签渲染,然后src要用vue-bind:src或者:src --><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>

23-Ajax-axios