javascript - Authorization token not sent with XMLHttpRequest
问题描述
Hi I'm trying to make a GET request using XMLHttpRequest. The endpoint that Im making the GET request to require an authorization token in the header (custom). When I try to make the request, the authorization token is not sent. Here is the code
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "/api/rest/v1/test",true);
xmlhttp.withCredentials = true;
xmlhttp.send(null);
This is a same site request.
解决方案
I don't see where your token is. Make sure that you are not misunderstanding sending a token with saying credentials true.
Once you already have the token, you could attach it as following:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', '/api/rest/v1/test');
xmlhttp.setRequestHeader('Authorization', 'Bearer ' + token);
xmlhttp.send(null);
推荐阅读
- c++ - 在 c++ 中使用向量的问题的解决方案在提交时显示,但在使用自定义输入运行相同的输入时运行良好
- elasticsearch - 用于弹性搜索的 Python API - 我们如何根据多个字段过滤搜索结果?
- python - 数据框列的数字很多是 str 格式,很多是浮点数,我如何将它们全部转换为浮点数
- html - 如何使宽度大于其容器的文本在 IE 11 中向左移动而不是向右移动?
- c# - X509Certificate2 无法解码 Xamarin.iOS 上的 .PFX 证书
- ansible - Ansible - 删除线
- sql - 使用 sql 窗口函数对过去 n 天的数量求和
- reactjs - 无法从 Web 浏览器安装 PWA
- javascript - router.js中的Vue js“未定义商店”
- angular - 我如何为这个角色服务创建一个模拟