axios和fetch哪个更好
Fetch API更好。它被定义在BOM的window对象中,你可以用它来发起对远程资源的请求。
基础axios用法
//依赖于axios对私有ajax进行修改import Qs from 'qs'import axios from 'axios'import router from 'router/index'import {errorPrompt, loading,
closeLoading} from 'util/util'export const status = { SUCCESS: '100', NET_ERR: '101', // BIZ_ERR: '103', // 业务请求异常
NO_AUTH: '104'}export function fetch(options) { return new Promise((resolve, reject) => { let instance = axios.create({
baseURL: process.env.BASE_API,
timeout: 2000,
headers: { // "tracecode": window.encodeURIComponent(JSON.stringify({"ua":"","cv":"20161230","token":"3dwo0onUUsPKVJcP8tk","os":"windows10","app":"kind","ws":"1*1","pkey":"f8caf7d7-a5d4-4710-b06f-28a922b6a467"}))
"tracecode": commonBizHeader(isManager(options)), 'Content-Type': 'application/x-www-form-urlencoded'
},
transformRequest: [function (data) { // Do whatever you want to transform the data let ret = ''
for (let it in data) {ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'}
2024-10-30 广告