Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中
- JS中原生提供了Ajax操作. 弊端: 操作特别的复杂 易用性较差.
- jQuery中的Ajax 封装了原生的JS Ajax 提高了开发的效率
- Axios是VUE中默认支持的Ajax的请求的方式.
Get-简单参数
/**
* GET请求-简单参数的写法
* 需求: 根据ID查询数据
* URL: http://localhost:8090/axios/getUserById?id=100
* then(): 回调函数通过then返回 结构
*/
axios.get("http://localhost:8090/axios/getUserById?id=100")
.then(function(result){
console.log(result.data)
})
Get-resultFul
/**
* restFul风格实现业务传参
* 需求: 根据name/age查询数据
* URL: http://localhost:8090/axios/user/tomcat/18
* 注意: 模版字符串优化参数 ``
*/
let name = "mysql"
let age = 20
axios.get(`http://localhost:8090/axios/user/${name}/${age}`)
.then(function(result){
console.log(result.data)
})
Axios-Get-对象传参
/**
* 需求: 实现对象方式的数据传递
* url: http://localhost:8090/axios/user/getUserObj
* 语法: axios.get("url","参数").then(回调函数)
* 业务需求: 查询name="mysql" age=18 sex="女"的用户
*/
//封装对象
let user = {
name: "mysql",
age: 18,
sex: "女"
}
axios.get(
"http://localhost:8090/axios/user/getUserObj",{params: user})
.then(function(result){
console.log(result.data)
})
/* axios.get("http://localhost:8090/axios/user/getUserObj",
{
//key: value key固定写法 params 参数对象
params: {
//再写用户的参数
name: "mysql",
age: 18,
sex: "女"
}
}).then(function(result){
console.log(result.data)
}) */
Axios-Delete
请求的参数和语法和get是一样的
1.不带参数的删除
axios.delete(“url地址”).then(function(result){ … })
2.携带个别参数 ?id=100
axios.delete(“url地址?id=100”).then(function(result){ … })
3.restFul结构
可以使用模版字符串的方式简化代码结构
axios.delete( “url地址/xxx/xxx/xxx”).then(function(result){ … })
4.采用对象的方式进行参数传递
let 对象 = {xxxx:xxxx,xxxx:xxxx}
axios.delete( “url地址/xxx/xxx/xxx”, {params: 封装后的对象}).then(function(result){ … })
Axios-post
/*
1.什么时候使用post请求????
答:一般采用form表单提交时,采用post请求类型
主要用于数据的新增操作
2.get请求/post请求主要的区别
get: 参数动态的拼接到URL地址中 ?id=xx&name=xxx 数据是可见的
post: 一般采用post请求数据是涉密的
*/
/**
* post业务:
* 实现用户的新增 传递User对象
*
* URL地址:
* http://localhost:8090/axios/insertUser
* 总结: 如果需要对象传参
* 1.get请求采用 axios.get(url,{params: 对象})
* 2.post请求 axios.post(url,对象)
*/
let user = {
name: "post请求的语法",
age: 20,
sex: '女'
}
let url1 = "http://localhost:8090/axios/insertUser"
axios.post(url1, user)
.then(function(result){
console.log(result.data)
})
Axios-post-restFul
/**
* post请求 restFul的写法
* 实现用户新增入库
* url: http://localhost:8090/axios/user/name/age/sex
*/
let url2 = "http://localhost:8090/axios/user/redis/18/男"
axios.post(url2)
.then(function(result){
console.log(result.data)
})
async-await用法
概念解释
1.async/await 是ES7引入的新语法 可以更加方便的进行异步操作
2.async 关键字用在函数上. 返回值是一个promise对象
3.await 关键字用在async 函数中
//配置基本请求路径
axios.defaults.baseURL = "http://localhost:8080/"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>async-await语法</title>
</head>
<body>
<h1>Axios练习</h1>
<!-- 引入JS文件 -->
<script src="../js/axios.js"></script>
<script>
/**
* axios的get请求语法
* 知识点:
* 1.箭头函数 主要简化回调函数的写法
* 思路: 重复的 固定的可以简化
* 规则: 如果参数只有一个则括号可以省略
*
* 2.async-await简化 解构赋值
* 2.1 async 需要标识函数
* 2.2 await 需要标识ajax请求
* 上述的操作可以将多行js 封装为一行执行 简化代码操作
*/
//1.定义一个方法
async function getUserById(){
let url = "http://localhost:8090/axios/getUserById?id=100"
//2.发起ajax请求 获取promise对象
//let promise = await axios.get(url)
//console.log(promise)
//console.log(promise.data)
//解构赋值 提取对象中有价值的数据
let {data: resultData,status: code} = await axios.get(url)
console.log(resultData)
console.log(code)
}
//2.调用方法
getUserById()
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>VUE-Axios练习</title>
</head>
<body>
<!-- 定义VUE根标签 -->
<div id="app">
<div align="center">
<h1 align="center">用户新增</h1>
名称: <input type="text" v-model.trim="addUser.name"/>
年龄: <input type="text" v-model.number="addUser.age"/>
性别: <input type="text" v-model.trim="addUser.sex"/>
<button @click="addUserMe">新增</button>
</div>
<hr />
<table id="tab1" border="1px" align="center" width="80%">
<tr>
<td colspan="5" align="center"><h1>用户列表</h1></td>
</tr>
<tr align="center">
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>操作</td>
</tr>
<!-- 指令: 可以循环数据+标签 v-for -->
<tr align="center" v-for="user in userList">
<td v-text="user.id"></td>
<td v-text="user.name"></td>
<td v-text="user.age"></td>
<td v-text="user.sex"></td>
<td>
<button>修改</button>
<button>删除</button>
</td>
</tr>
</table>
</div>
<!-- 1.引入VUE.js -->
<script src="../js/vue.js"></script>
<!-- 2.引入Axios.js -->
<script src="../js/axios.js"></script>
<!--
需求分析1:
1.当用户打开页面时就应该发起Ajax请求获取userList数据.
2.将userList数据 在页面中展现
2.1页面中的数据必须在data中定义
2.2 ajax请求的结果赋值给data属性
3.利用v-for指令实现数据遍历
需求分析2: 用户数据入库操作
1.在页面中准备用户新增文本框/按钮
2.准备双向数据绑定的规则
3.为按钮添加事件,实现数据新增入库.
-->
<script>
//设定axios请求前缀
axios.defaults.baseURL = "http://localhost:8090"
const app = new Vue({
el: "#app",
data: {
//1.定义集合数据 null
userList: [],
//2.定义addUser对象 新增的用户数据
addUser: {
name: '',
age: 0,
sex: ''
}
},
methods: {
//1.定义getuserList方法 获取后台服务器数据
async getUserList(){
let{data: result} = await axios.get("/vue/getUserList")
//ajax调用之后,将数据给属性.
this.userList = result
},
//新增操作 请求类型: post 接收时需要使用json方式处理
async addUserMe(){
//不需要返回值
await axios.post("/vue/insertUser",this.addUser)
//将列表页面重新刷新
this.getUserList()
}
},
//调用生命周期函数
mounted(){
//console.log("vue对象实例化成功!!!!")
//初始化时调用getUserList()
this.getUserList()
}
})
</script>
</body>
</html>