axios

基于OAuth2.0的token无感知刷新

纵饮孤独 提交于 2020-04-15 09:50:40
【推荐阅读】微服务还能火多久?>>>   目前手头的vue项目关于权限一块有一个需求,其实架构师很早就要求我做了,但是由于这个紧急程度不是很高,最近临近项目上线,我才想起,于是赶紧补上这个功能。这个项目是基于OAuth2.0认证,需要在每个请求的头部携带access_token,如果这个access_token过期,需要利用已有的refresh _token去重新获取一个access_token,如果连这个refresh_token也过期了,那就是真正的过期了,需要退出登录页面。refresh_token在获取新的access_token的时候需要让用户无感知,也叫无痛刷新。   这里的代码实现肯定是要在axios拦截器里写的,但是是在请求拦截器里写还是在响应拦截器里写还是有区别的:   1.写在请求拦截器里:每次请求之前都会先请求一个checkToken的接口,来确认这个access_token是否过期,如果没有过期,直接就发起原本的请求,如果过期,利用已有的refresh _token去重新获取一个access_token之后,再发起原本的请求。但是这样写有个缺点,就是每次请求之前都要额外请求一次checkToken的接口,如果网速不好,会给用户造成不好的体验,而且对服务器造成了性能上的浪费。   2.写在响应拦截器里:直到access_token过期,返回401未授权

React 中的生命周期函数

白昼怎懂夜的黑 提交于 2020-04-15 09:30:07
【推荐阅读】微服务还能火多久?>>> 生命周期函数指的是组件在某一时刻会自动执行的函数 constructor可以看成一个类的普通生命周期函数,但不是react独有的生命周期函数 render() 是数据发生变化时会自动执行的函数,因此属于react的生命周期函数 mounting只在第一次渲染时会执行 import React,{Component} from 'react' ; class Counter extends Component{ constructor(props){ super(props); console.log( 'constructor' ); } componentWillMount(){ console.log( 'componentWillMount' ); } componentDidMount(){ console.log( 'componentDidMount' ); } render(){ console.log( 'render' ); return ( <div>hello react</div> ) } } export default Counter; 可以看到代码有提示:componentWillMount has been renamed, and is not recommended for use. 这是因为React 16

浅谈webpack+vue从零开始的后台管理项目(二) ----- vue项目基础功能完善

徘徊边缘 提交于 2020-04-14 17:44:34
【推荐阅读】微服务还能火多久?>>> 前言 上一篇讲了使用webpack初始化构建vue项目, 那么这一篇接着上一篇的内容继续讲一下,element的引入, axios请求载入, router路由载入和vuex状态管理 一.公用样式初始化重置 在讲公用样式初始化重置之前, 首先需要知道为什么要样式重置. 为了兼容性考虑,每个浏览器默认样式不一样,比如行高,某个浏览器是1,另外zd一个浏览器可能是1.1,再有一个浏览器可能就是0.9,这样在开发网页写样式表的时候,处理起来回比较麻烦,所以直接给重答置统一,这样就能很方便的处理兼容性。 所以, 通常我们在开发项目时, 都会在如前文中的style->common.js中进行一些样式重置.来保证后续开发中一些不必要的影响. 样式重置代码就不写了, 有很多, 如 body, dl, dd, h1, h2, h3, h4, h5, h6, p, form{ margin : 0 ;} ol,ul{ margin : 0 ; padding: 0 ;} a:focus, a :active { outline : none; } a, a :focus, a :hover { cursor : pointer; color: inherit; text-decoration: none; } 复制代码 二.Element的引入

基于OAuth2.0的token无感知刷新

感情迁移 提交于 2020-04-13 15:14:36
【今日推荐】:为什么一到面试就懵逼!>>>   目前手头的vue项目关于权限一块有一个需求,其实架构师很早就要求我做了,但是由于这个紧急程度不是很高,最近临近项目上线,我才想起,于是赶紧补上这个功能。这个项目是基于OAuth2.0认证,需要在每个请求的头部携带access_token,如果这个access_token过期,需要利用已有的refresh _token去重新获取一个access_token,如果连这个refresh_token也过期了,那就是真正的过期了,需要退出登录页面。refresh_token在获取新的access_token的时候需要让用户无感知,也叫无痛刷新。   这里的代码实现肯定是要在axios拦截器里写的,但是是在请求拦截器里写还是在响应拦截器里写还是有区别的:   1.写在请求拦截器里:每次请求之前都会先请求一个checkToken的接口,来确认这个access_token是否过期,如果没有过期,直接就发起原本的请求,如果过期,利用已有的refresh _token去重新获取一个access_token之后,再发起原本的请求。但是这样写有个缺点,就是每次请求之前都要额外请求一次checkToken的接口,如果网速不好,会给用户造成不好的体验,而且对服务器造成了性能上的浪费。   2.写在响应拦截器里:直到access_token过期,返回401未授权

axios 和 element的使用

爱⌒轻易说出口 提交于 2020-04-12 12:12:47
https://www.cnblogs.com/jflalove/p/11944483.html https://blog.csdn.net/qq_40236722/article/details/88175015 axios的使用:            /** * * 使用axios的步骤; * 第一步安装 npm install --save axios vue-axios 第二步,在mian.js中导入和配置, (这是全局的axios)     引入: import axios from 'axios' Vue.use(axios)     配置 : axios.defaults.baseURL="https://wd4363557576ckikwp.wilddogio.com/" 第三步在你要使用的文件下引用axios */     第二种配置    import axios from 'axios'   与很多第三方模块不同的是,axios不能使用use方法,转而应该进行如下操作     axios 发送数据:              const fromData={ age:this.ruleForm.age, pass:this.ruleForm.pass } //发送数据 axios.post('./axiosT.json',fromData) .then

Vue项目中使用Mockjs造假数据

本小妞迷上赌 提交于 2020-04-12 08:34:23
需求场景: 要get请求接口'/api/userInfo/list' 项目准备: 首先,安装项目中mockjs包、axios npm install mockjs axios 然后,src根 目录 下新建mock文件夹和mock.js、urls.js (目录结构不强求,主要还是看你项目的划分。也可以单独出一个request文件,放axios.js、mock.js、和专门整理接口用的url.js) user.vue文件中 ,接口请求代码(以axios为例): <template> <div class="api-element"> <div class="area"> <el-table :data="userTableData" > <el-table-column fixed label="日期" prop="date" width="150"></el-table-column> <el-table-column label="姓名" prop="name" width="120"></el-table-column> <el-table-column label="省份" prop="province" width="120"></el-table-column> <el-table-column label="市区" prop="city" width="120"></el

How to mock axios.create([config]) function to return its instance methods instead of overriding them with mock?

纵饮孤独 提交于 2020-04-12 07:47:08
问题 I'm trying to mock axios.create() because I'm using its instance across the app and obviously need all of its implementation which is destroyed by the mock, thus cannot get the result of the get, post method properly. This is how the code looks like in the actual file: export const axiosInstance = axios.create({ headers: { ...headers }, transformRequest: [ function (data, headers) { return data; }, ], }); const response = await axiosInstance.get(endpoint); And here is the mock setup for axios

How to mock axios.create([config]) function to return its instance methods instead of overriding them with mock?

烂漫一生 提交于 2020-04-12 07:47:06
问题 I'm trying to mock axios.create() because I'm using its instance across the app and obviously need all of its implementation which is destroyed by the mock, thus cannot get the result of the get, post method properly. This is how the code looks like in the actual file: export const axiosInstance = axios.create({ headers: { ...headers }, transformRequest: [ function (data, headers) { return data; }, ], }); const response = await axiosInstance.get(endpoint); And here is the mock setup for axios

Vue 异步请求

℡╲_俬逩灬. 提交于 2020-04-11 14:55:57
vue最初使用vue-resource来实现异步请求(ajax),vue 2.0开始推荐使用 axios 来代替vue-resource。 准备工作 1、使用npm下载axios npm install axios 2、引入axios.js < script src ="js/axios.js" ></ script > 上线时换为min.js 前端 vue使用axios发起异步请求 可以这样写: <! DOCTYPE html > < html > < head > < meta charset ="utf-8" /> < title ></ title > <!-- 引入vue.js --> < script src ="js/vue.js" ></ script > <!-- 引入axios.js --> < script src ="js/axios.js" ></ script > </ head > < body > < div id ="app" ></ div > < script > new Vue({ el: ' #app ' , template:` < div > < button @click = " login " > 发送 < / button> < / div> `, data(){ return { } }, methods:{ login:

missing 1 required positional argument: 'pk'

不打扰是莪最后的温柔 提交于 2020-04-11 06:46:08
问题 I am new in Django and react. I already faced this error at last week and That times its was request URL error. yesterday I changed backend design and now its error happens again. Here is my url=> urlpatterns = [ url(r'^allowances_mas/', AllowanceAPIView.as_view()), url(r'^allowances_mas/(?P<pk>\d+)/$', AllowanceAPIView.as_view()),.... here is my put method that inside view, def put(self,request,pk): save_allowance = get_object_or_404(Allowance.objects.all(),pk=pk) data = request.data.get(