axios

axios拦截器封装与使用

空扰寡人 提交于 2020-08-11 06:34:12
拦截器封装 import axios from "axios" // 创建axios 赋值给常量service const service = axios.create(); // 添加请求拦截器(Interceptors) service.interceptors.request.use(function (config) { // 发送请求之前做写什么 return config; }, function (error) { // 请求错误的时候做些什么 return Promise.reject(error); }); // 添加响应拦截器 service.interceptors.response.use(function (response) { // 对响应数据做点什么 return response; }, function (error) { // 对响应错误做点什么 return Promise.reject(error); }); export default service 使用 import service from "@/util/axiosin.js" export function registerdata(obj) { let pre = new Promise((resolve, reject) => { service.request({ url

vue项目经验:图形验证码接口get请求处理

送分小仙女□ 提交于 2020-08-11 06:30:42
     一般图形验证码处理: 直接把 img 标签的 src 指向这个接口,然后在 img 上绑定点击事件,点击的时候更改 src 的地址(在原来的接口地址后面加上随机数即可,避免缓存) < img :src ="codeImg" class ="img-code" @click ="updateCode" alt ="验证码" title ="点击换一张" > export default { data () { codeImg: `${ this .baseUrl}/captcha/ captcha.php }, methods: { updateCode() { this .codeImg = `${ this .baseUrl}/captcha/captcha.php?= ${Math.random()}`; } } }      但是,有一天,后端说,在接口的响应头里放了一些信息,需要提交form表单时,一并提交。然后用axios的get请求,尴尬了,响应的是数据流,显示不出图片了。      解决方案如下:将数据流转换为图片   首先html结构不变,把js改了。 export default { data () { imgCode: '', // 一定要有 captchaId: '' // 后端需要的响应头中的信息参数 }, created () { this

用axios的post方式实现下载

送分小仙女□ 提交于 2020-08-10 23:43:27
亲测有效 dowloadTest ( ) { this . $axios . request ( { method : 'post' , url : '/api/filedue' , data : { fileid : 123 } , responseType : 'arraybuffer' } ) . then ( ( response ) => { this . download ( response . data ) ; } ) } , download ( data ) { if ( ! data ) { return } let url = window . URL . createObjectURL ( new Blob ( [ data ] ) ) let link = document . createElement ( 'a' ) link . style . display = 'none' link . href = url link . download = 'ea7c0cf24153e0cd62bc8b64841fd84d.jpg' ; //下载后文件名 link . setAttribute ( 'download' , 'ea7c0cf24153e0cd62bc8b64841fd84d.jpg' ) document . body .

Axios: How to check for cookie in browser before sending API request?

给你一囗甜甜゛ 提交于 2020-08-10 22:49:14
问题 Goal: In the browser code, check that a cookie is present before sending a specific request that always requires credentials. The cookie contains a JWT token. Other Details: Vue.js (used for front-end code in browser) Axios (used for all API calls to back-end API) JWT Token (inside the cookie) Cookie set with "http-only" by the server that creates it Checking for cookie existence prior to sending a request is good. It would be even better to check if the cookie containing the JWT token is

Axios: How to check for cookie in browser before sending API request?

試著忘記壹切 提交于 2020-08-10 22:47:13
问题 Goal: In the browser code, check that a cookie is present before sending a specific request that always requires credentials. The cookie contains a JWT token. Other Details: Vue.js (used for front-end code in browser) Axios (used for all API calls to back-end API) JWT Token (inside the cookie) Cookie set with "http-only" by the server that creates it Checking for cookie existence prior to sending a request is good. It would be even better to check if the cookie containing the JWT token is

How to fix TypeError: Cannot read property 'post' of undefined on Axios with Nestjs cronjob

帅比萌擦擦* 提交于 2020-08-10 19:37:15
问题 I tried using cron scheduler to get authentication token every 15 sec(Test purpose) the cron is supposed to call the auth endpoint but I got Exception has occurred: TypeError: Cannot read property 'post' of undefined @Cron(CronExpression.EVERY_15_SECONDS) async handleCron() { //const Primetimeauth = this.PrimetimeAuth() const primeAuth = await this.httpService.post('https://clients.com/api/auth', { "username": process.env.username, "password": process.env.password }).toPromise(); if

How to fix TypeError: Cannot read property 'post' of undefined on Axios with Nestjs cronjob

佐手、 提交于 2020-08-10 19:36:34
问题 I tried using cron scheduler to get authentication token every 15 sec(Test purpose) the cron is supposed to call the auth endpoint but I got Exception has occurred: TypeError: Cannot read property 'post' of undefined @Cron(CronExpression.EVERY_15_SECONDS) async handleCron() { //const Primetimeauth = this.PrimetimeAuth() const primeAuth = await this.httpService.post('https://clients.com/api/auth', { "username": process.env.username, "password": process.env.password }).toPromise(); if

Axios FormData() getting empty Object

本秂侑毒 提交于 2020-08-10 19:00:30
问题 Browser-side code let data = new FormData(); data.append('file', file); data.append('userId', userId); axios.post(`${baseUrl}/uploadFile`, data, {headers: {'Content-Type':'multipart/form-data'}}).then((result) => console.log(result)).catch((err) => cb(err)) Server side code app.use((req, res, next) => { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT, PATCH, OPTIONS'); res.setHeader('Access-Control-Allow-Headers',

Vue- axios baseURL

断了今生、忘了曾经 提交于 2020-08-10 18:20:38
Vue- axios baseURL的使用方法 每次 Api 都要填写域名,可以设置一个 baseUrl,这样少写点代码,而且后面更换域名时,生产环境和开发环境统一时很有用 qs.stringify 将参数格式化成 QueryString,可以不设置 在 main.js 中修改: Vue . prototype . axios = axios . create ( { baseURL : '/api/' , transformRequest : [ //对数据转换成类似get传参的模式 data = > qs . stringify ( data ) ] } ) ``` 如果想自己开发一个管理应用系统,使用 vue 来开发前端,Laravel 做后台,对于个人独立开发,达成目标更可期。 还觉得: Laravel 的教程还没完全消化,vue 又开始了,总觉得我一瓶子不满、半瓶子咣当。郑州下雪了,外面很安静,我也该放弃满脑子的杂念,去静静,毕竟又是一年,无奈大部分时间精力被工作占去,没去做想去专注的事情。 最后,过去的一年觉得: 不知是谁讲的猎人思维,管理、销售上驾驭人的想法被普及地遍地都是,每个人都想把你当成自己资源,这当是一种” 人性 “的竞争吧。今年要给自己身上的” 接口 “加锁,稳如磐石,不动如钟,少让外物忽悠吧!把剩余时间都留给学习。 来源: oschina 链接:

全栈的自我修养: 001环境搭建 (使用Vue,Spring Boot,Flask,Django 完成Vue前后端分离开发)

社会主义新天地 提交于 2020-08-10 13:25:04
全栈的自我修养: 环境搭建 Not all those who wander are lost. 彷徨者并非都迷失方向。 Table of Contents @ 目录 前言 环境准备 nodejs vue-cli 创建 Vue 项目 yarn和npm 命令 对照表 项目结构 使用 elementUI 配置 Vuex 配置 axios github 参考 当你看到这篇文章的时候,暂且认为你对如何做一个网站有了兴趣. 前言 本系列文章将从一个完整的项目是如何开发的过程进行编写,期间会涉及前端、后端和一些运维的知识。 本篇题为 全栈的自我修养 将通过一个项目整合( 一前端项目对应三个后端项目 ),完成一个简单的DEMO 其中前端项目使用 Vue.js ,这个项目将会用到 vue , vuex , vue-route , axios , elementUI 等 后端项目使用为 3 个项目,其中涉及 Spring Boot, Mybaits, Flask 等 中间会穿插一些运维的知识如 常用linux命令, Jenkins 等 也会介绍一些工具的使用 计划分为以下几个项目: epimetheus-frontend 面向用户的PC前端项目 epimetheus-management-frontend 面向运营人员的内部管理系统前端项目 epimetheus-miniapp-frontend