我使用Axios来执行如下的HTTP post:
import axios from 'axios'
params = {'HTTP_CONTENT_LANGUAGE': self.language}
headers = {'header1': value}
axios.post(url, params, headers)
这对吗?或者我应该这样做:
axios.post(url, params: params, headers: headers)
我使用Axios来执行如下的HTTP post:
import axios from 'axios'
params = {'HTTP_CONTENT_LANGUAGE': self.language}
headers = {'header1': value}
axios.post(url, params, headers)
这对吗?或者我应该这样做:
axios.post(url, params: params, headers: headers)
当前回答
您还可以为每个axios请求设置选定的头信息:
// Add a request interceptor
axios.interceptors.request.use(function (config) {
config.headers.Authorization = 'AUTH_TOKEN';
return config;
});
第二种方法
axios.defaults.headers.common['Authorization'] = 'AUTH_TOKEN';
其他回答
你可以传递一个配置对象给axios,比如:
axios({
method: 'post',
url: '....',
params: {'HTTP_CONTENT_LANGUAGE': self.language},
headers: {'header1': value}
})
你可以初始化一个默认头axios.defaults.headers
axios.defaults.headers = {
'Content-Type': 'application/json',
Authorization: 'myspecialpassword'
}
axios.post('https://myapi.com', { data: "hello world" })
.then(response => {
console.log('Response', response.data)
})
.catch(e => {
console.log('Error: ', e.response.data)
})
试试这段代码
在示例代码中使用axios get rest API。
在安装
mounted(){
var config = {
headers: {
'x-rapidapi-host': 'covid-19-coronavirus-statistics.p.rapidapi.com',
'x-rapidapi-key': '5156f83861mshd5c5731412d4c5fp18132ejsn8ae65e661a54'
}
};
axios.get('https://covid-19-coronavirus-statistics.p.rapidapi.com/v1/stats?
country=Thailand', config)
.then((response) => {
console.log(response.data);
});
}
希望就是帮助。
我在申请职位时遇到了这个问题。我在axios header中做了如下更改。它工作得很好。
axios.post('http://localhost/M-Experience/resources/GETrends.php',
{
firstName: this.name
},
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
var axios = require("axios").default;
var options = {
method: 'GET',
url: 'https://api.pexels.com/v1/curated',
params: {page: '2', per_page: '40'},
headers: {Authorization: '_authkey_'}
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});