import axios from 'axios'; const http = axios.create({ // withCredentials: true, // crossDomain: true, baseURL: 'http://localhost:3000', timeout: 30000 }) //请求拦截,在每个请求发出去之前,针对每个域名做不同的配置 http.interceptors.request.use(config => { if (config.requestBase == 'VUE_APP_URL') { config.headers['Content-Type'] = "application/x-www-form-urlencoded"; } else if (config.requestBase == 'VUE_APP_URL_TWO') { config.headers['Content-Type'] = "application/json"; config.baseURL = process.env.VUE_APP_URL_TWO; config.data = JSON.stringify(config.data); } else if (config.requestBase == 'VUE_APP_URL_THREE') { config.baseURL = process.env.VUE_APP_URL_THREE; } return config; }, error => Promise.error(error)) // 响应拦截器 http.interceptors.response.use( // 请求成功 res => ( console.log('拦截器', res),res.status === 200 ? Promise.resolve(res.data) : Promise.reject(res.data)), // 请求失败 error => { const { response } = error; if (response) { return Promise.reject(response); } else { // 处理断网的情况 // eg:请求超时或断网时,更新state的network状态 // network状态在app.vue中控制着一个全局的断网提示组件的显示隐藏 // 关于断网组件中的刷新重新获取数据,会在断网组件中说明 // store.commit('changeNetwork', false); } } ); // 真机调试 axios.defaults.adapter = function (config) { return new Promise((resolve, reject) => { console.log(config) var settle = require('axios/lib/core/settle'); var buildURL = require('axios/lib/helpers/buildURL'); uni.request({ method: config.method.toUpperCase(), url: buildURL(config.url, config.params, config.paramsSerializer), header: config.headers, data: config.data, dataType: config.dataType, responseType: config.responseType, sslVerify: config.sslVerify, complete:function complete(response){ response = { data: response.data, status: response.statusCode, errMsg: response.errMsg, header: response.header, config: config }; settle(resolve, reject, response); } }) }) } export default http
axios h5可以 微信开发errMsg: "request:fail invalid url?这是H5的 访问没问题 也有返回数据 [图片] 这是微信开发者工具的 [图片]
2021-06-17