
【vue】利用axios发送请求
- 一、项目环境配置
- 二、利用axios发送POST请求登录
- 三、异步实现:利用axios发送POST请求登录(json)
- 四、异步实现:利用axios发送POST请求登录(表单)
- 五、token存储
- 六、token使用
- 七、全局的axios配置
-
- 八、请求拦截器和响应拦截器
一、项目环境配置
根组件App.vue

路由配置

main.js配置
固定写法,复制粘贴即可

二、利用axios发送POST请求登录
| <template> |
| <div class="box1"> |
| <el-form label-width="120px"> |
| <el-form-item label="用户名"> |
| <el-input v-model="loginForm.username"></el-input> |
| </el-form-item> |
| <el-form-item label="密码"> |
| <el-input type="password" v-model="loginForm.password"></el-input> |
| </el-form-item> |
| <el-form-item> |
| <el-button style="width:100%" type="primary" @click="submitLogin">登录</el-button> |
| </el-form-item> |
| </el-form> |
| </div> |
| |
| </template> |
| |
| <script> |
| import axios from 'axios' |
| |
| |
| |
| |
| |
| export default{ |
| data(){ |
| return{ |
| loginForm:{ |
| username:"", |
| password:"" |
| } |
| } |
| }, |
| methods:{ |
| |
| submitLogin(){ |
| console.log('点击了登录,',this.loginForm) |
| |
| const params={ |
| username:this.loginForm.username, |
| password:this.loginForm.password |
| } |
| |
| const res = axios.post('http://82.156.178.247:5001/users/login/',params) |
| console.log('res',res) |
| |
| |
| |
| res.then(response=>{ |
| console.log('请求成功:') |
| console.log('respnse',response) |
| }) |
| |
| res.catch(error=>{ |
| console.log('请求失败:') |
| |
| console.log('请求失败响应对象获取',error.response) |
| }) |
| } |
| } |
| } |
| |
| </script> |
| |
| <style> |
| |
| </style> |
复制
请求失败时,控制台输出的对象

请求成功时,控制台输出的对象

三、异步实现:利用axios发送POST请求登录(json)
| <template> |
| <div class="box1"> |
| <el-form label-width="120px"> |
| <el-form-item label="用户名"> |
| <el-input v-model="loginForm.username"></el-input> |
| </el-form-item> |
| <el-form-item label="密码"> |
| <el-input type="password" v-model="loginForm.password"></el-input> |
| </el-form-item> |
| <el-form-item> |
| <el-button style="width:100%" type="primary" @click="submitLogin">登录</el-button> |
| </el-form-item> |
| </el-form> |
| </div> |
| |
| </template> |
| |
| <script> |
| import axios from 'axios' |
| |
| |
| |
| |
| |
| export default{ |
| data(){ |
| return{ |
| loginForm:{ |
| username:"", |
| password:"" |
| } |
| } |
| }, |
| methods:{ |
| |
| async submitLogin(){ |
| const params={ |
| username:this.loginForm.username, |
| password:this.loginForm.password |
| } |
| |
| const response =await axios.post('http://82.156.178.247:5001/users/login/',params) |
| console.log('response:',response) |
| |
| if (response.status===200){ |
| this.$message({ |
| type:"success", |
| message:"登录成功" |
| }); |
| |
| this.$router.push({name:"index"}) |
| } |
| }, |
| |
| } |
| } |
| |
| </script> |
| |
| <style> |
| |
| </style> |
复制
四、异步实现:利用axios发送POST请求登录(表单)

| <template> |
| <div class="box1"> |
| <el-form label-width="120px"> |
| <el-form-item label="用户名"> |
| <el-input v-model="loginForm.username"></el-input> |
| </el-form-item> |
| <el-form-item label="密码"> |
| <el-input type="password" v-model="loginForm.password"></el-input> |
| </el-form-item> |
| <el-form-item> |
| <el-button style="width:100%" type="primary" @click="submitLogin">登录</el-button> |
| </el-form-item> |
| </el-form> |
| </div> |
| |
| </template> |
| |
| <script> |
| import axios from 'axios' |
| import qs from 'qs' |
| |
| |
| |
| |
| export default{ |
| data(){ |
| return{ |
| loginForm:{ |
| username:"", |
| password:"" |
| } |
| } |
| }, |
| methods:{ |
| |
| async submitLogin(){ |
| |
| const params = qs.stringify(this.loginForm) |
| |
| const response =await axios.post('http://82.156.178.247:5001/users/login/',params) |
| console.log('response:',response) |
| |
| if (response.status===200){ |
| this.$message({ |
| type:"success", |
| message:"登录成功" |
| }); |
| |
| this.$router.push({name:"index"}) |
| } |
| }, |
| |
| } |
| } |
| |
| </script> |
| |
| <style> |
| |
| </style> |
复制
五、token存储
保存登录返回的token
获取token
const token=response.data.token
保存到LocalStorage中:永久存储,只有不手动删除,永久保存到LocalStorage中
window.localStorage.setItem(‘token’,token)
保存到sessionStorage中:关闭浏览器之后,删除内容
window.sessionStorage.setItem(‘token’,token)
保存到cookie中:
window.cookieStore.set(‘token’,token)
| <template> |
| <div class="box1"> |
| <el-form label-width="120px"> |
| <el-form-item label="用户名"> |
| <el-input v-model="loginForm.username"></el-input> |
| </el-form-item> |
| <el-form-item label="密码"> |
| <el-input type="password" v-model="loginForm.password"></el-input> |
| </el-form-item> |
| <el-form-item> |
| <el-button style="width:100%" type="primary" @click="submitLogin">登录</el-button> |
| </el-form-item> |
| </el-form> |
| </div> |
| |
| </template> |
| |
| <script> |
| import axios from 'axios' |
| import qs from 'qs' |
| |
| |
| |
| |
| export default{ |
| data(){ |
| return{ |
| loginForm:{ |
| username:"", |
| password:"" |
| } |
| } |
| }, |
| methods:{ |
| |
| async submitLogin(){ |
| |
| const params={ |
| username:this.loginForm.username, |
| password:this.loginForm.password |
| } |
| |
| const response =await axios.post('http://82.156.178.247:5001/users/login/',params) |
| console.log('response:',response) |
| |
| if (response.status===200){ |
| this.$message({ |
| type:"success", |
| message:"登录成功" |
| }); |
| |
| this.$router.push({name:"index"}) |
| |
| |
| const token=response.data.token |
| |
| |
| |
| |
| |
| window.cookieStore.set('token',token) |
| } |
| }, |
| |
| } |
| } |
| |
| </script> |
| |
| <style> |
| |
| </style> |
复制
六、token使用
| <template> |
| <h1>index页面</h1> |
| <el-button @click="getAllProject">获取项目数据</el-button> |
| |
| <h3>{{pros}}</h3> |
| </template> |
| |
| <script> |
| import axios from 'axios' |
| |
| export default{ |
| data(){ |
| return{ |
| pros:{ |
| |
| } |
| } |
| }, |
| methods:{ |
| async getAllProject(){ |
| |
| const url="http://82.156.178.247:5001/projects/" |
| const response=await axios.get(url,{ |
| headers:{ |
| 'Authorization':'JWT ' + window.sessionStorage.getItem('token') |
| } |
| }); |
| console.log('response',response); |
| this.pros=response.data |
| } |
| } |
| } |
| </script> |
| |
| <style> |
| </style> |
复制

七、全局的axios配置
在api/index.js中对axios对后端请求进行统一封装

1、方式一
| |
| import axios from "axios" |
| |
| |
| |
| axios.defaults.baseURL='http://82.156.178.247:5001' |
| axios.defaults.headers.common['kobe']='0924' |
| |
| export const login=(params)=>{ |
| axios.post('/login',params) |
| } |
| |
| export const getALLPro=()=>{ |
| axios.get('/login') |
| } |
| |
复制
2、方式二
| |
| |
| const http=axios.create({ |
| |
| baseURL:'http://82.156.178.247:5001', |
| |
| validateStatus:function(status){ |
| return true; |
| } |
| }) |
| |
| export default{ |
| login(params){ |
| return http.post('/login/',params) |
| }, |
| getAllProject(){ |
| return http.get('/projects/') |
| |
| } |
| } |
复制
在main.js中导入index.js,作为全局对象,方便以后的开发项目中使用
$api
为属性名称

八、请求拦截器和响应拦截器
| |
| http.interceptors.request.use(function (config){ |
| |
| |
| if(config.url!=='/login/'){ |
| config.headers['Authorization']='JWT ' + window.sessionStorage.getItem('token') |
| } |
| |
| |
| console.log('请求拦截器:',config) |
| |
| |
| return config |
| }); |
| |
| |
| |
| http.interceptors.response.use(function(response){ |
| |
| console.log('响应拦截器:',response) |
| |
| return response; |
| |
| }) |
复制
