/**
* @author huangteng
* @description a new ajax func sync
* @version 1.0.0
* @param url 地址
* @param body 参数对象
* @param method 访问方式
* @note: 最佳实践是用es6语法,这里使用的es5
* @modified by:
* @modified time:
*/
var xhr = function(url,body,method){
this.url = url
this.body = body
this.method = method || 'GET'
var defer = $.Deferred()
$.ajax({
type: this.method,
url: this.url,
data: this.body,
beforeSend: function(request){
request.setRequestHeader('Content-type','application/json')
}
})
.done(defer.resolve)
.fail(defer.reject)
return defer.promise()
}
/**
* @description how to use
* @note 支持链式调用
*/
xhr('http://dxxxx.com1',{name:'jack',pwd:'123'},'POST').then(function(req){
console.log(req)
}).fail(function(err){
console.log(err)
})
一个不一样的ajax
最新推荐文章于 2024-04-23 23:57:52 发布