适用于对老项目维护时,用习惯的axios不能使用的情况

基础封装: 保留 then 的回调、baseHref、method 传 post || get || etc,

function ajax(obj) {
var callback = $.ajax({
url: window.baseHref + obj.url,
type: obj.method || "post",
data: obj.data,
dataType: 'json',
beforeSend: function(request) {
request.setRequestHeader("X-CSRF-TOKEN", window.csrf);
},
});
callback.then = function(res, rej) {
res && callback.done(res);
rej && callback.fail(rej);
return callback;
}
return callback;
}

  

例子,请求成功:

ajax({
url: '/webbanner',
method: 'post',
}).then(function(res) {
console.log(res)
}, function(rej) {
console.log('rej1')
}).then(function(res) {
console.log('res2')
}) // output
// 请求结果
// 'res2'

  

例子,请求失败:

ajax({
url: '/11111111',
method: 'post',
}).then(function(res) {
console.log(res)
}, function(rej) {
console.log('rej1')
}).then('', function(rej) {
console.log('rej2')
}) // output
// 'rej1'
// 'rej2'

  

随机推荐

  1. 【java并发编程艺术学习】(三)第二章 java并发机制的底层实现原理 学习记录(一) volatile

    章节介绍 这一章节主要学习java并发机制的底层实现原理.主要学习volatile.synchronized和原子操作的实现原理.Java中的大部分容器和框架都依赖于此. Java代码 ==经过编译= ...

  2. Android开发者学习必备:10个优质的源码供大家学习

    最近看了一些开发的东西,汇总了一些源码.希望可以给大家有些帮助! 1.Android 源码解析—PagerSlidingTabStrippagerSlidingTabStrip 实现联动效果的原理是, ...

  3. “box-shadow”属性(转)

    “box-shadow”属性 box-shadow属性是一个CSS3属性,允许我们在几乎任何元素上来创建阴影效果,类似于在设计软件中的”drop shadow”.这些阴影效果允许我们在原本平面的.二维 ...

  4. 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  5. 杭电acm 1033题

    Problem Description For products that are wrapped in small packings it is necessary that the sheet o ...

  6. Hadoop YARN配置参数剖析(3)—MapReduce相关参数

    MapReduce相关配置参数分为两部分,分别是JobHistory Server和应用程序参数,Job History可运行在一个独立节点上,而应用程序参数则可存放在mapred-site.xml中 ...

  7. p4180 次小生成树

    传送门 分析: 次小生成树的求法有两种,最大众的一种是通过倍增LCA找环中最大边求解,而这里我介绍一种神奇的O(nlogn) 做法: 我们先建立最小生成树,因为我们用kruskal求解是边的大小已经按 ...

  8. Bootstrap栅格学习

    参考:https://segmentfault.com/a/1190000000743553 节选翻译自The Subtle Magic Behind Why the Bootstrap 3 Grid ...

  9. linux下oracle一些常用命令

    dbca 配置数据库netca 配置tnslsnrctl status tns状态lsnrctl stop TNS停止lsnrctl start TNS启动

  10. Shape Number (最小表示法)

    题目链接 一个字符串,这个字符串的首尾是连在一起的,要求寻找一个位置,以该位置为起点的字符串的字典序在所有的字符串中中最小. #include <bits/stdc++.h> using ...