23-Ajax-axios
一、原生Ajax
<!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>
二、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>
三、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>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgaebhf
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24