- 支持客户端和服务端
- 支持abort,cache,timeout,retry,interceptor等
- 扩展更加方便,通过中间件,可自定义扩展(当前扩展例子有:cache, retry, 文件上传)
Ajax.get(url: string, params?: ParamsData, options?: AjaxOptions)
Ajax.delete(url: string, params?: ParamsData, options?: AjaxOptions)
Ajax.post(url: string, body?: BodyData, params?: ParamsData, options?: AjaxOptions)
Ajax.put(url: string, body?: BodyData, params?: ParamsData, options?: AjaxOptions)
// 总入口, 可自定义请求如:options,head等, retry就是interceptor + request实现
Ajax.request(requestArgs: RequestArgs);
支持url变量替换,其中变量使用方式是 :变量明
Ajax.get('/api/:id/find-name', {id: 'aaaaa', name: 'autobest'})
// 等价于
Ajax.get('/api/aaaaa/find-name', {name: 'autobest'})
// 等价于
Ajax.get('/api/aaaaa/find-name?name=autobest')
export const defaultConfig: AjaxGlobalConfig = {
timeout: 0,
responseType: 'json',
requestConfig: {
cache: 'no-store', // 解决IE11下缓存问题
credentials: 'same-origin',
referrerPolicy: 'no-referrer-when-downgrade',
headers: {
'If-Modified-Since': '0' // 解决IE11下缓存问题
},
mode: 'cors',
redirect: 'follow'
}
};
// 非formData, file, 默认是Content-Type
headers['Content-Type'] = 'application/json';