将vue,axios,qs的js文件引入到代码中 写好界面按钮信息以及按钮绑定事件
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.20.0/axios.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/qs/6.9.4/qs.js"></script> <title>axios</title> </head> <body> <div id="app"> <!-- <div>{{response}}</div> --> <button @click="findAllCustomers">查询所有顾客信息</button> <button @click="findCustomerById">根据ID获取顾客信息</button> <button @click="login">登录</button> <button @click="queryCustomers">分页查询顾客信息</button> </div> </body>查询所有顾客信息属于get请求无参数情况 根据ID获取顾客信息属于get请求有参数情况 登录属于post请求无参数情况 分页查询顾客信息属于post请求有参数情况
url获取路径,method获取请求方式
findAllCustomers() { axios({ url: "http://120.26.177.210:5588/customer/findAll", method: "get", params: {} }).then((res) => { console.log(res.data.data) this.response = res.data.data }) },params获取请求参数,没有参数就为空
findCustomerById() { let baseURL = "http://120.26.177.210" axios({ method: 'get', url: baseURL + "/customer/findCustomerById", params: { id: 134 } }).then((res) => { console.log(res.data.data) this.response = res.data.data }) },传递的数据放到obj对象中
login() { let obj = { password: 123321, type: "customer", username: "susan" } axios({ method: 'post', url: "http://120.26.177.210/user/login", data: obj }).then((res) => { console.log(res.data.data) }) },qs.stringify()将json对象转换成查询字符串
queryCustomers() { let obj = { page: 0, pageSize: 10 } axios({ method: 'post', url: "http://120.26.177.210/customer/query", data: Qs.stringify(obj) }).then((res) => { console.log(res.data.data.list) }) }