<!--
Asynchronous JavaScript And Xml,异步的JavaScript和Xml
作用:
数据交换:通过Ajax可以给服务器发送请求,并获得服务器响应的数据
异步交互:可以在不重新加载整个页面的情况下,与服务器交换数据并更新部分网页
步骤:
1.创建XMLHttpRequest对象
2.发送异步请求
3.获取服务响应数据
Axios对原生ajax进行封装
使用:
引入Axios的js文件
使用Axios发送请求,并获取响应结果
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./Axios/axios-0.18.0.js"></script>
<script src="../day02_vue/vue/vue.js"></script>
</head>
<body>
<div id="app">
<input type="button" value="获取数据GET" @click="get()">
<input type="button" value="删除数据POST" @click="post()">
</div>
</body>
<script>
new Vue({
el: "#app",
methods: {
get: function () {
// 通过axios发送异步请求-get
axios({
method: "get",
url: "http://yapi.smart-xwork.cn/mock/169327/emp/list"
}).then(
(result) => {
console.log(result.data);
}
)
},
post: function () {
// 通过axios发送异步请求-post
// axios({
// method:"post",
// url:"http://yapi.smart-xwork.cn/mock/169327/emp/deleteById",
// data:"id=1"
// }).then(
// (result)=>{
// console.log(result.data);
// }
// )
}
},
mounted() {
// 简化写法
// axios.post("url","id=1").then((res)=>{console.log();})
// post get delete put
}
})
</script>
</html>
Javaweb Axios
于 2023-07-21 19:54:47 首次发布