npm install axios -S
axios({
url: ‘http://123.207.32.32:8000/home/multidata‘,
method: ‘pos‘
}).then(res => console.log(res))
baseURL = ‘http://123.207.32.32:8000‘
timeout = 5000
method = ‘get‘
params = {type: ‘pop‘,page: 1}
axios.all([
axios({url: ‘/home/multidata‘}),
axios({url: ‘/home/data‘, params: {type: ‘pop‘,page: 1}})
]).then(axios.spread(res1, res2) => {
console.log(res1)
console.log(res2)
})
const instance1 = axios.create({
baseURL: ‘http://123.207.32.32:8000‘,
timeout: 500
})
instance1({
url: ‘/home/multidata‘
}).then(res => console.log(res))
//request.js
import axios from ‘axios‘
export function request(config) {
const instance = axios.create({
baseURL: ‘http://123.207.32.32:8000‘,
timeout: 5000
})
// 拦截器
// ……
return instance(config)
}
import {
request
} from ‘./network/request.js‘
request({
url: ‘/home/multidata‘
}).then(res => console.log(res))
.catch(err => console.log(err))
export function request(config) {
// 1.创建做axios实例
const instance = axios.create({
baseURL: ‘http://123.207.32.32:8000‘,
timeout: 5000
})
axios.interceptors.request.use(config => {
console.log(config)
// 1.比如config中的一些不符合服务器的要求
// 2.比如每次发送网络请求时,都希望在界面中显示一个请求的图标
// 3.某些网络请求(比如登录),必须携带一些特殊的信息
// 必须要返回拦截的对象
return config
}, err => {
return err
})
instance.interceptors.response.use(res => {
// console.log(res)
return res.data
}, err => {
console.log(err)
return err
})
//3、发送真正的网络请求
return instance(config)
原文:https://www.cnblogs.com/jincanyu/p/14352376.html