Access-Control-Allow-Origin不允许使用非本地文件的Origin为null

Origin null is not allowed by Access-Control-Allow-Origin using not local files

本文关键字:文件 Origin null 不允许 Access-Control-Allow-Origin      更新时间:2023-09-26

我有以下脚本,我想只做一件事。调用一个url并检查它是否为http。状态200返回。我一直得到"原点null是不允许访问-控制-允许-原点"的错误。我在StackOverflow上看到了一些与本地文件系统有关的事情,但是,我调用http url。谁知道为什么我得到这个错误。我用的是Chrome浏览器。提前感谢你的帮助!

<html>
<head>
<script type="text/javascript">
function checkHet(invoer)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.write("Klopt als een bus!");
}
else
{
document.write("Klopt <b>niet</b> als een bus!");
}
}
xmlhttp.open("GET", invoer, true );
xmlhttp.send();  
}           
</script>
</head>
<body>
<form>
<input type="text" name="adres" value="http://www.">
<input type="button" value="Check" onClick="checkHet(form.adres.value)">
</form>
</body>
</html>

因为出于安全考虑,不允许在其他域中(包括本地文件系统)使用AJAX。这是同源策略(SOP)的作用。

但是也有例外,特别是如果目标域允许跨域资源共享(CORS)

  • 本地文件不能被Javascript访问。因为如果javascript可以访问本地文件,javascript可能会窃取客户端的文件。
  • 通常,当一个脚本试图访问本地文件时,浏览器会弹出一个安全对话框,询问用户是否允许脚本继续。