Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 无法将HTTP标头放入变量中_Reactjs_Http_Axios_Xmlhttprequest_Http Headers - Fatal编程技术网

Reactjs 无法将HTTP标头放入变量中

Reactjs 无法将HTTP标头放入变量中,reactjs,http,axios,xmlhttprequest,http-headers,Reactjs,Http,Axios,Xmlhttprequest,Http Headers,我正在构建一个带有Spring Boot REST API和React前端的全堆栈web应用程序,但有些事情困扰着我。我希望尽量减少代码重复,但我似乎不能将axios请求的HTTP头放在一个变量中,并在所有请求中重复使用它。请求只是使用变量失败 这项要求适用于: axios.get(baseUrl + "interests/user?id=" + localStorage.getItem('userId'), { headers: { with

我正在构建一个带有Spring Boot REST API和React前端的全堆栈web应用程序,但有些事情困扰着我。我希望尽量减少代码重复,但我似乎不能将axios请求的HTTP头放在一个变量中,并在所有请求中重复使用它。请求只是使用变量失败

这项要求适用于:

axios.get(baseUrl + "interests/user?id=" + localStorage.getItem('userId'), { headers: {
                withCredentials: true,
                authorization: 'Bearer ' + localStorage.getItem('token')
            }}
        )
此请求不起作用:

const headers = {
 headers: {
                withCredentials: true,
                authorization: 'Bearer ' + localStorage.getItem('token')
            }
}

axios.get(baseUrl + "interests/user?id=" + localStorage.getItem('userId'), { headers }
        )
有人能向我解释为什么这不起作用,以及可能如何解决它吗?提前谢谢