Javascript 如何在 axios 中设置标题和选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45578844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set header and options in axios?
提问by user2950593
I use Axios to perform an HTTP post like this:
我使用 Axios 执行这样的 HTTP 帖子:
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)
回答by riyaz-ali
There are several ways to do this:
做这件事有很多种方法:
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 requestsFor 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; });
对于单个请求:
let config = { headers: { header1: value, } } let data = { 'HTTP_CONTENT_LANGUAGE': self.language } axios.post(URL, data, config).then(...)设置默认全局配置:
axios.defaults.headers.post['header1'] = 'value' // for POST requests axios.defaults.headers.common['header1'] = 'value' // for all requests要在 axios 实例上设置为默认值:
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; });
回答by roli roli
You can send a get request with Headers (for authentication with jwt for example):
您可以发送带有 Headers 的 get 请求(例如使用 jwt 进行身份验证):
axios.get('https://example.com/getSomething', {
headers: {
Authorization: 'Bearer ' + token //the token is a variable which holds the token
}
})
Also you can send a post request.
您也可以发送post请求。
axios.post('https://example.com/postSomething', {
email: varEmail, //varEmail is a variable which holds the email
password: varPassword
},
{
headers: {
Authorization: 'Bearer ' + varToken
}
})
My way of doing it,is to set a request like this:
我的做法是设置这样的请求:
axios({
method: 'post', //you can set what request you want to be
url: 'https://example.com/request',
data: {id: varID},
headers: {
Authorization: 'Bearer ' + varToken
}
})
回答by sjc42002
You can pass a config object to axios like:
您可以将配置对象传递给 axios,例如:
axios({
method: 'post',
url: '....',
params: {'HTTP_CONTENT_LANGUAGE': self.language},
headers: {'header1': value}
})
回答by gtamborero
This is a simple example of a configuration with headers and responseType:
这是带有标头和响应类型的配置的简单示例:
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 can be 'application/x-www-form-urlencoded' or 'application/json' and it may work also 'application/json;charset=utf-8'
内容类型可以是 'application/x-www-form-urlencoded' 或 'application/json' 也可以是 'application/json;charset=utf-8'
responseType can be 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
responseType 可以是“arraybuffer”、“blob”、“document”、“json”、“text”、“stream”
In this example, this.data is the data you want to send. It can be a value or an Array. (If you want to send an object you'll probably have to serialize it)
在本例中,this.data 是您要发送的数据。它可以是一个值或一个数组。(如果你想发送一个对象,你可能需要序列化它)
回答by Morris S
You can initialize a default header axios.defaults.headers
您可以初始化默认标头 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)
})
回答by Rishith Poloju
if you want to do a get request with params and headers.
如果您想使用参数和标头执行 get 请求。
var params = {
paramName1: paramValue1,
paramName2: paramValue2
}
var headers = {
headerName1: headerValue1,
headerName2: headerValue2
}
Axios.get(url, {params, headers} ).then(res =>{
console.log(res.data.representation);
});
回答by Prateek Arora
Here is the Right way:-
这是正确的方法:-
axios.post('url', {"body":data}, {
headers: {
'Content-Type': 'application/json'
}
}
)
回答by superup
try this code
试试这个代码
in example code use axios get rest API.
在示例代码中使用 axios get rest API。
in mounted
在安装
mounted(){
var config = {
headers: {
'x-rapidapi-host': 'covid-19--statistics.p.rapidapi.com',
'x-rapidapi-key': '5156f83861mshd5c5731412d4c5fp18132ejsn8ae65e661a54'
}
};
axios.get('https://covid-19--statistics.p.rapidapi.com/v1/stats?
country=Thailand', config)
.then((response) => {
console.log(response.data);
});
}
Hope is help.
希望是帮助。
回答by Najathi
I have face this issue in post request. I have changed like this in axios header. It works fine.
我在 post request 中遇到过这个问题。我在 axios 头文件中做了这样的改变。它工作正常。
axios.post('http://localhost/M-Experience/resources/GETrends.php',
{
firstName: this.name
},
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

