本文介绍了如何在 axios 中设置标题和选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Axios 执行这样的 HTTP 帖子:
I use Axios to perform an HTTP post like this:
import axios from 'axios' params = {'HTTP_CONTENT_LANGUAGE': self.language} headers = {'header1': value} axios.post(url, params, headers)这是正确的吗?或者我应该这样做:
Is this correct? Or should I do:
axios.post(url, params: params, headers: headers) 推荐答案有几种方法可以做到这一点:
There are several ways to do this:
对于单个请求:
For a single request:
用于设置默认全局配置:
For setting default global config:
axios.defaults.headers.post['header1'] = 'value' // for POST requests axios.defaults.headersmon['header1'] = 'value' // for all requests用于在 axios 实例上设置为默认值:
For setting as default on axios instance:
let instance = axios.create({ headers: { post: { // can be common or any other method header1: 'value1' } } }) //- or after instance has been created instance.defaults.headers.post['header1'] = 'value' //- or before a request is made // using Interceptors instance.interceptors.request.use(config => { config.headers.post['header1'] = 'value'; return config; });更多推荐
如何在 axios 中设置标题和选项?
发布评论