Javascript:“请求的资源上不存在‘Access-Control-Allow-Origin’标头。因此不允许访问Origin‘null’。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24336593/
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
Javascript: " No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. "
提问by user3761728
I need to write a web page that can get some information I need with JavaScript. The sever is a bamboo sever and I am trying to utilize the REST API of theirs in my JavaScript. https://developer.atlassian.com/display/BAMBOODEV/REST+APIs
我需要编写一个可以使用 JavaScript 获取一些我需要的信息的网页。服务器是一个竹子服务器,我试图在我的 JavaScript 中利用他们的 REST API。 https://developer.atlassian.com/display/BAMBOODEV/REST+APIs
The weird part is that I can perform all the requests by typing the link in the browser, or with curl command in termanal. It also works via a python script. I can receive the data with all the methods mentioned in the paragraph. It's just that when I go to JavaScript, it doesn't work anymore.
奇怪的是,我可以通过在浏览器中输入链接或在终端中使用 curl 命令来执行所有请求。它也可以通过 python 脚本工作。我可以使用段落中提到的所有方法接收数据。只是当我使用 JavaScript 时,它不再起作用了。
I have been following the information in the below link for making the CORS request. http://www.codeproject.com/Articles/185506/AJAX-Cross-Origin-HTTP-request
我一直在关注以下链接中的信息以提出 CORS 请求。 http://www.codeproject.com/Articles/185506/AJAX-Cross-Origin-HTTP-request
Attached below is my code. I was wondering if I have missed something.
下面附上我的代码。我想知道我是否错过了什么。
<!doctype html>
<html>
<head>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="jquery-1.11.1.js"></script>
<script src="crypto-js.js"></script>
<script type="text/javascript">
function showResponse (response) {
RESPONSE = response;
if (this && this.url && (typeof(this.url) == "string")) {
var anchor = jQuery("#url");
anchor.text(this.url.toString());
anchor.attr('href', this.url.toString());
}
jQuery("#output").text(JSON.stringify(response, null, ' '));
}
</script>
<script type="text/javascript">
console.log("a")
var cor = null; // cor stands for Cross-Origin request
if (window.XMLHttpRequest) {
console.log("support xmlhttprequest");
cor = new XMLHttpRequest();
}else {
console.log("Your browser does not support Cross-Origin request!");
}
if("withCredentials" in cor){
cor.open('GET', 'http://bamboo.example.com/rest/api/latest/stuff', true);
cor.withCredentials = true;
cor.setRequestHeader('Authorization', 'Basic encoded_stuff');
cor.send();
}
cor.onload = function(){
var responseText = xhr.responseText;
console.log(responseText);
};
cor.onerror = function() {
console.log('There was an error!');
};
</script>
</head>
<body>
<div>URL:<a id="url"></a></div>
<hr>
<div>
<pre id="output">
<!-- content will appear here -->
</pre>
</div>
</body>
</html>
The chrome console shows "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access." as a result along with 401 status.
chrome 控制台显示“请求的资源上不存在 'Access-Control-Allow-Origin' 标头。因此不允许访问 Origin 'null'。” 结果与 401 状态一起。
Was wondering if I am missing with the code?
想知道我是否缺少代码?
Thank you
谢谢
回答by Noble Mushtak
It seems that you're sending a request to this REST API link from some HTML file that's not on the same domain. Since there's no "Access-Control-Allow-Origin" header on that link and you're sending a request to it from a different domain, for security reasons, you won't get a coherent response from it, but instead will get that error and a 401 error since you're unauthorized to look at the contents of that link.
您似乎是从某个不在同一域中的 HTML 文件向此 REST API 链接发送请求。由于该链接上没有“Access-Control-Allow-Origin”标头,并且您从不同的域向它发送请求,出于安全原因,您不会从它那里得到一致的响应,而是会得到那个错误和 401 错误,因为您无权查看该链接的内容。
There's unfortunately no way around that; that link is simply not meant to be requested to from outside domains.
不幸的是,没有办法解决这个问题。该链接根本不打算从外部域请求。