我使用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)
有几种方法可以做到这一点:
For a single request:
let config = {
headers: {
header1: value,
}
}
let data = {
'HTTP_CONTENT_LANGUAGE': self.language
}
axios.post(URL, data, config).then(...)
For setting default global config:
axios.defaults.headers.post['header1'] = 'value' // for POST requests
axios.defaults.headers.common['header1'] = 'value' // for all requests
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;
});
你可以通过带有头部信息的GET请求(例如使用JWT进行身份验证)来发送请求:
axios.get('https://example.com/getSomething', {
headers: {
Authorization: 'Bearer ' + token //the token is a variable which holds the token
}
})
您也可以发送POST请求。
axios.post('https://example.com/postSomething', {
email: varEmail, //varEmail is a variable which holds the email
password: varPassword
},
{
headers: {
Authorization: 'Bearer ' + varToken
}
})
我的做法是这样设置请求:
axios({
method: 'post', //you can set what request you want to be
url: 'https://example.com/request',
data: {id: varID},
headers: {
Authorization: 'Bearer ' + varToken
}
})
axios.post('url', {"body":data}, {
headers: {
'Content-Type': 'application/json'
}
}
)
header
是单数还是复数形式声明的。从你的回答中,这对我很有帮助。 - 钟智强axios({
method: 'post',
url: '....',
params: {'HTTP_CONTENT_LANGUAGE': self.language},
headers: {'header1': value}
})
Here is the Right way:-
axios.post('url', {"body":data}, {
headers: {
'Content-Type': 'application/json'
}
}
)
这是一个包含头部和响应类型的配置的简单示例:
var config = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
responseType: 'blob'
};
axios.post('http://YOUR_URL', this.data, config)
.then((response) => {
console.log(response.data);
});
Content-Type 可以是 'application/x-www-form-urlencoded' 或者 'application/json', 也可以使用 'application/json;charset=utf-8'。
responseType 可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'。
在这个例子中,this.data 是你想要发送的数据。它可以是一个值或一个数组。 (如果你想要发送一个对象,你可能需要将它序列化)
您可以初始化默认标题 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
请求:
// Add a request interceptor
axios.interceptors.request.use(function (config) {
config.headers.Authorization = 'AUTH_TOKEN';
return config;
});
第二种方法
axios.defaults.headers.common['Authorization'] = 'AUTH_TOKEN';
var params = {
paramName1: paramValue1,
paramName2: paramValue2
}
var headers = {
headerName1: headerValue1,
headerName2: headerValue2
}
Axios.get(url, {params, headers} ).then(res =>{
console.log(res.data.representation);
});
尝试这段代码。
在示例代码中使用axios获取REST API。
在mounted生命周期函数中。
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);
});
}
希望就是帮助。