适用于对老项目维护时,用习惯的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. Microsoft Data Access Components 2.8

    今天在安装金蝶系统的时候,发现这个组件无法安装上 此组件如果跳过,不安装,将会导致金蝶安装完毕后,提醒无法创建账套,也就是金蝶软件无法连接到数据库上,所以此步为必须. 于是乎,网上找这个组件,下载地址 ...

  2. 问题:HttpWebRequest request post 传参; 结果:好用的C# HttpWebRequest用Post同时提交参数和文件的封装类

    在项目中,本来都是在站内进行数据交互的,后来又加进来一个买的php网站(艹).需要进行数据交互,在没有考虑使用web服务的情况下,只有通过Post提交到页面进行数据交互是最好的方式了. 我这边使用的是 ...

  3. HTML5 中文乱码

    <meta charste="utf-8"> 只是告诉浏览器要用utf-8来解释,而文档的编码,是在你保存时的选择决定的.如果保存ANSI 然后用utf-8解释,肯定是 ...

  4. CodeForces 492A Vanya and Cubes

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

    DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...

  6. 19.Imagetragick 命令执行漏洞(CVE-2016–3714)

    Imagetragick 命令执行漏洞(CVE-2016–3714) 漏洞简介: Imagetragick 命令执行漏洞在16年爆出来以后,wooyun上面也爆出了数个被该漏洞影响的大厂商,像腾讯, ...

  7. 《鸟哥的Linux私房菜》读书笔记4

    1. grep查找 grep 'string' filename; last | grep 'root'; 以行为单位. 利用参数-i(忽略大小写),-v(反相)等进行正则表达式: ‘’中可以为正则表 ...

  8. 在Ionic Framework 退出APP

    使用以下代码: ionic.Platform.exitApp(); 在控制器中增加: $scope.Quit = function(){ ionic.Platform.exitApp(); }

  9. 图像特征--HOG特征

    1.HOG特征:方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的梯 ...

  10. App集成极光推送开发流程[关键步骤]

    1.客户端集成SDK 1.1初始化 JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志 JPushInterface.init(this); / ...