如何将基本身份验证标头分配给XMLHTTPREQUEST? [英] How to assign basic authentication header to XMLHTTPREQUEST?

查看:127
本文介绍了如何将基本身份验证标头分配给XMLHTTPREQUEST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多关于预检和CORS的答案,所以请不要发布引用我应该阅读的内容的链接。许多答案来自服务器视角,但在这种情况下我是客户端。我是否设置了原始标题?我的假设是这是一个简单的请求,我是对的吗?

I've read many answers of preflight and CORS so please do not post links referencing what I should read. Many of the answers are from a server-perspective, but I am the client in this case. Do I set the origin header? My assumption is that this is a simple request, am I right?

 req.open("POST", url, true);
 req.setRequestHeader( 'Content-Type',   'application/blahblah' );
 req.setRequestHeader( 'Accept', 'application/blahblah' );
 req.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass)); 
 req.send();

但它仍然无效,我的错误:

Yet its still not working, my error:


对预检请求的响应未通过访问控制检查:请求的资源上没有Access-Control-Allow-Origin标头。因此不允许原点'null'访问。响应的HTTP状态代码为500.

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 500.


推荐答案

解决方案:

req.setRequestHeader('Authorization', 'Basic [base64 encoded password here]' );

这篇关于如何将基本身份验证标头分配给XMLHTTPREQUEST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆