一个不一样的ajax

xiaoxiao2021-02-28  92

/** * @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) })
转载请注明原文地址: https://www.6miu.com/read-44343.html

最新回复(0)