axios统一设置请求头
可以使用axios的拦截器来统一设置请求头。
可以在创建axios实例之前使用axios的defaults.headers来设置默认的请求头:
axios.defaults.headers.common['Authorization'] = 'Bearer token';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
也可以使用axios的拦截器来在发送请求之前统一设置请求头:
axios.interceptors.request.use(function (config) {
config.headers['Authorization'] = 'Bearer token';
config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
return config;
}, function (error) {
return Promise.reject(error);
});
以上两种方法都可以实现统一设置请求头,根据自己的需求选择其中一种方法即可
原文地址: https://www.cveoy.top/t/topic/ijxH 著作权归作者所有。请勿转载和采集!