一、axios响应数据结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { // `data` 由服务器提供的响应 data: {}, // `status` 来自服务器响应的 HTTP 状态码 status: 200, // `statusText` 来自服务器响应的 HTTP 状态信息 statusText: 'OK', // `headers` 服务器响应的头 headers: {}, // `config` 是为请求提供的配置信息 config: {}, // 'request' 请求信息 request: {} }
|
vue.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ transpileDependencies: true, devServer:{ port:8081, proxy: { '/xx':{ target:'http://localhost:8081', //需要跨域的url ws:true, //代理webSocket changeOrigin:true, //允许跨域 pathRewrite:{ '^/xx':'' //重写路径 } } } } })
|
DevView.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <template> <el-row class="mb-4"> <el-button type="primary">添加</el-button> <el-button type="success">删除</el-button> <el-button type="success">编辑</el-button> </el-row>
<el-divider/>
<el-table :data="tableData" style="width: 100%"> <el-table-column prop="id" label="主键" width="180"/> <el-table-column prop="username" label="用户名" width="180"/> <el-table-column prop="password" label="密码" width="180"/> <el-table-column prop="status" label="status"/> </el-table>
</template>
<script> import axios from "axios";
export default { name: "DevView", data(){ //model return { tableData : [] } }, created() { axios({ method: "get", url: "/xx/users" }).then((respDate)=>{ console.log(respDate) this.tableData = respDate.data }).catch((error)=>{ console.log(error) }) } } </script>
<style scoped>
</style>
|
二、测试
后台接口:http://localhost:8081/users
![](/./axios%E5%AD%A6%E4%B9%A0/a1.png)