我正在尝试请求 api 来获取 token 。 该 token 位于 RespondHeader 中,但我的代码找不到它。但我用curl得到了它,所以肯定我错过了一些东西。 你能帮我吗?
这是 curl :
curl -i -X POST -H "Content-Type:application/json"http://127.0.0.1:8080/login -d '{"用户名":"poulet", "密码":"poulet"}'
这就是我得到的
HTTP/1.1 200
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJwb3VsZXQiLCJleHAiOjE1NTEzMDUxNDJ9.NTKyMKEKTnRDU-TzcG6WlNYVCjgQ91vBgK4SbTFECenRH_GCllxA-dPogx3RQ0XH0eCwH7LpCU8Ttyxb2idl_Q
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Length: 0
Date: Tue, 26 Feb 2019 22:05:42 GMT
现在这是我的 Javascript 代码
const req = new XMLHttpRequest();
req.open('POST', 'http://127.0.0.1:8080/login', true);
req.setRequestHeader("Content-Type", "application/json");
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.getAllResponseHeaders());
console.log(this.getResponseHeader("Authorization"));
}
};
console.log(req);
req.send(JSON.stringify({username : "poulet", password : "poulet"}));
这就是我得到的
cache-control: no-cache, no-store, max-age=0, must-revalidate expires: 0 pragma: no-cache
null
提前谢谢您。
最佳答案
如果人们有同样的问题。这是服务器,它没有跨域,因此缺少一些 header 。
我通过添加来解决它
访问控制允许来源:*
谢谢
关于javascript - XMLHTTPRequest 错误响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54894963/