可以通過向 axios
傳遞相關配置來創(chuàng)建請求:
// 發(fā)送 POST 請求
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
})
// 獲取遠端圖片
axios({
method: 'get',
url: 'http://bit.ly/2mTM3nY',
responseType: 'stream'
})
.then(function(response){
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'));
})
// 發(fā)送 GET 請求(默認的方法)
axios('/user/12345');
為方便起見,為所有支持的請求方法提供了別名
注意:在使用別名方法時, url
、method
、data
這些屬性都不必在配置中指定。
處理并發(fā)請求的助手函數(shù):
可以使用自定義配置創(chuàng)建一個 axios 實例
axios.create([config])
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
})
更多建議: