#AJAX
AJAX,用法同jQuery.ajax()
Example
const ajax = plug('ajax');
ajax.proxy(request,response).request({
url:url,
dataType: 'json',
data: data
}).done(function(d){
const json = d.result;
});
Example
const ajax = plug('ajax');
(async function(){
const json = await ajax.proxy(request,response).request({
url:url,
dataType: 'json',
data: data
}).toES6Promise().then((d)=>{
return d.result;
}).catch(()=>{
return null;
});
})();
Example
const ajax = plug('ajax');
ajax.proxy(request,response).request({
url:url,
dataType: 'proxy'
}).fail(function(d){
response.end();
});
Ajax.proxy(req,res) -- 返回一个新的Ajax实例Ajax.request(opt) -- 发起一次http请求Ajax.proxy(req,res).request(opt) -- 代理模式,自动继承req.headersAjax.request(opt) -- 非代理模式,空headersreturn -- Deferred实例,可以通过.toES6Promise()转为ES6 Promise对象optdevIp -- string 开发者模式下,覆盖ipdevPort -- number 开发者模式下,覆盖portip -- string 目标ipport -- number 目标port,默认80host -- string 目标域名useIPAsHost -- boolean 使用IP覆盖headers.host,默认falsetype -- "GET"|"POST",忽略大小写url -- string 目标url,优先覆盖host、port、pathpath -- string 目标pathheaders -- object 要发送的headerstimeout -- number 超时时间,默认值2000msretry -- number 重试次数,默认值1data -- object 要发送的数据,GET请求追加到url后在,POST请求转为bodybody -- string|Buffer http bodyencodeMethod -- string 格式化参数时的编码函数,默认encodeURIComponentsuccess -- function 成功回调error -- function 失败回调response -- function(response) response事件回调response -- 请求对应的response对象返回值 -- 返回false可中断ajax处理流程formatCode -- function(obj,opt){return {isFail: 0,code:0}} 格式化返回码obj -- 原始data,类型与dataType对应isFail -- 0:成功,1:失败,2:逻辑失败code -- 翻译后的返回码dataType -- string|number 指定返回值类型,默认值htmlhtml -- 以string形式处理回包,gzip自动解压text -- 以string形式处理回包,gzip自动解压buffer -- 以Buffer形式处理回包,gzip不会自动解压json -- 以string形式处理回包,并转成json,gzip自动解压jsonp -- 以string形式处理回包,并转成json,gzip自动解压302 -- response.statusCode 为302时触发成功,忽略回包404 -- response.statusCode 为404时触发成功,忽略回包proxy -- 用于透明代理,自动处理返回结果到response里,只需处理error回调即可jsonpCallback -- string 用于指定jsonp回调的名字useJsonParseOnly -- boolean 强制使用JSON.parse解析回包,默认值false,请求不受信任数据来源时建议开启false -- 不强制,此时可以请求jsonp和不严格的jsontrue -- 强制,这样更安全,同时要求JSON严格按标准套路来send -- function(request) 自定义发送内容,覆盖bodyenctype -- string POST时指定body的编码方式application/x-www-form-urlencoded -- 默认值application/jsonmultipart/form-datakeepAlive -- boolean 是否启用长连接,默认falsedataopt -- object 即原始optbuffer -- buffer 原始响应内容result -- buffer|string|json 根据dataType转换后的结果responseText -- string 原始响应内容hasError -- boolean 请求过程中是否发生网络错误或超时code -- number 错误码,出错时填写msg -- string 出错信息response -- object 原始response对象times -- object 各种测速打点start -- Date 开始请求response -- Date response事件触发end -- Date end事件触发data.code508 -- Parse json error502 -- 建立tcp出错,对端原因600 -- window.response提前关闭60X -- 600+剩余重试次数513 -- 超时51X -- 513+剩余重试次数