vue-axios当只调用vue.js又需要axios请求多时
可以将axios方法封装一个函数
(function () {
ASK = {
get:function (url,data,succFun,errFun) {
axios.get(url,{
params:data,
headers:{
"token":""
}
})
.then(function (response) {
if(response.data.code=='200'){
if (succFun){
succFun(response.data);
}
}else if(response.data.code=='401'){
alter('请求超时,请重新登录')
window.location.href='login.html'
}else{
console.log(response.data)
}
})
.catch(function (error) {
if (errFun){
errFun(error);
}
});
},
post:function (url,data,succFun,errFun) {
axios.post(url,data,{
headers:{
"token":"",
'Content-Type':'application/x-www-form-urlencoded'
}
})
.then(function (response) {
if(response.data.code=='200'){
if (succFun){
succFun(response.data);
}
}else if(response.data.code=='401'){
alter('请求超时,请重新登录')
window.location.href='login.html'
}else{
console.log(response.data)
}
})
.catch(function (error) {
if (errFun){
errFun(error);
}
});
},
other:function (url,requestType,data,succFun,errFun) {
axios({
method: requestType,
url: url,
data: data,
headers:{
"token":""
}
}).then(function (response) {
console.log(response);
if (succFun){
succFun(response.data);
}
}).catch(function (error) {
console.log(error);
if (errFun){
errFun(error);
}
})
}
};
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
/**
* 请求之前操作:success
*/ return config;
}, function (error) {
/**
* 请求之前操作:failure
*/ return Promise.reject(error);
}); // 添加响应拦截器
axios.interceptors.response.use(function (response) {
/**
* 响应之前操作:success
*/ return response;
}, function (error) {
/**
* 响应之前操作:failure
*/ return Promise.reject(error);
});
})(axios);
调用时
ASK.get(CDK.host+'/cdk/company/findList',{
"pageNum":this.pageNum,
"pageSize":this.pageSize
},(res)=>{
console.log(res.obj)
},(err)=>{
console.log(err)
})
vue-axios当只调用vue.js又需要axios请求多时的更多相关文章
- iOS 或者Android调用vue.js 里面的方法
1.原生调用vue.js 某个vue组件下的方法. 比如**.vue里面有个这样的方法: 如果这样的话,在iOS或者Android里面是调用不了这个ajax方法的. 需要在**.vue (我的版本是v ...
- vue使用案例,vue初始化,vue初始化方法,vue条件语句,vue在js里面添加元素调用vue方法
<div id="main" > <button id='but1' type="button" v-on:click="save ...
- vue中配置axios.js文件,发送请求
为了统一管理请求,每个项目都会去配置axios:而不是在vue中直接使用,那样不好维护等等 下面是我配置的最基础的axios文件 第一步:首先新建一个axios文件,我是放在router文件下的 im ...
- js和vue方法的相互调用(iframe父子页面的方法相互调用)。
项目是前后端不分离的,模板引擎使用的JSP. 但是使用了Vue+ElementUI,这里列举一些常用的调用方式,有时候可能. 在js里调用vue方法 我们需要把方法注册到vue对象之外的页面,所以对与 ...
- Vue项目中使用基于Vue.js的移动组件库cube-ui
cube-ui 是滴滴公司的技术团队基于 Vue.js 实现的精致移动端组件库.很赞,基本场景是够用了,感谢开源!感谢默默奉献的你们. 刚爬完坑,就来总结啦!!希望对需要的朋友有小小的帮助. (一)创 ...
- vue之注册自定义的全局js方法
前端开发的时候,总会需要写一些js方法,在vue框架中为了方便使用,可以考虑注册一个全局的js方法,下面是注册步骤: 1.0 可以在assets文件中的js文件下面新建一个js文件,如:yun.js- ...
- vue axios接口封装、Promise封装、简单的axios方法封装、vue接口方法封装、vue post、get、patch、put方法封装
相信大家在做前后端数据交互的时候都会给请求做一些简单的封装就像之前封装ajax方法一样axios的封装也是一样的简单下面这个就是封装的axios的方法,require.js import axios ...
- 基于Vue.js PC桌面端弹出框组件|vue自定义弹层组件|vue模态框
vue.js构建的轻量级PC网页端交互式弹层组件VLayer. 前段时间有分享过一个vue移动端弹窗组件,今天给大家分享一个最近开发的vue pc端弹出层组件. VLayer 一款集Alert.Dia ...
- html中创建并调用vue组件的几种方法
最近在写项目的时候,总是遇到在html中使用vue.js的情况,且页面逻辑较多,之前的项目经验都是使用脚手架等已有的项目架构,使用.vue文件完成组价注册,及组件之间的调用,还没有过在html中创建组 ...
随机推荐
- TZOJ 4359: Partition the beans (二分)
描述 Given an N x N square grid (2 <= N <= 15) and each grid has some beans in it. You want to w ...
- GCD使用经验与技巧浅谈
前言 GCD(Grand Central Dispatch)可以说是Mac.iOS开发中的一大“利器”,本文就总结一些有关使用GCD的经验与技巧. dispatch_once_t必须是全局或stati ...
- LeetCode99 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- mybatis分页插件pageHelper简单实用
工作的框架spring springmvc mybatis3 首先使用分页插件必须先引入maven依赖,在pom.xml中添加如下 <!-- 分页助手 --> <dependency ...
- 关于使用JavaMail发送邮件
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.activati ...
- python 数据集变量的数据类型总结
- cume_dist(),允许并列名次、复制名次自动空缺,取并列后较大名次,结果如22355778……
将score按ID分组排名:cume_dist() over(partition by id order by score desc)*sum(1) over(partition by id) 将sc ...
- 自定义View系列教程06--详解View的Touch事件处理
深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...
- 【codeforces 798D】Mike and distribution
[题目链接]:http://codeforces.com/contest/798/problem/D [题意] 让你选一个下标集合 p1,p2,p3..pk 使得2*(a[p1]+a[p2]+..+a ...
- mysql数据库之linux版本
http://repo.mysql.com/yum/mysql-5.6-community/ 安装 ...