【发布时间】:2020-11-12 20:51:27
【问题描述】:
这是一个常见问题(请参阅末尾的编号链接),但我不知道如何解决。
我在下面包含了我的 javascript 代码。我正在使用 MAMP PRO,并在 htdocs 文件夹中添加了一个文件 '.htaccess',其内容为 'Header set Access-Control-Allow-Origin "http://localhost:8888"' 并重置 MAMP 服务器但我得到同样的错误。
有人可以建议我哪里出错了吗?如果您需要更多信息,请告诉我,我对此很陌生..
function loadComments(callback){
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();// code for IE7+, Firefox, Chrome, Opera, Safari
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");// code for IE6, IE5
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (JSON.parse(this.responseText)[0]==null) {
} else {
alert(JSON.parse(this.responseText));
}
}
};
xmlhttp.open("GET","http://localhost:8888/projectX/projectX.php?webpageURL=" + "https://theguardian.com/au" ,false);
xmlhttp.send();
}
- The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin error
- The 'Access-Control-Allow-Origin' header has a value 'https://dev.getevents.co' that is not equal to the supplied origin
- The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin
- The 'Access-Control-Allow-Origin' header has a value 'http://localhost:4200' that is not equal to the supplied origin
【问题讨论】:
-
您正在向
http://localhost:8888发出请求。那不是你的出身。来源很可能是您地址栏中的域。 -
嘿,Ivar,这不是一个实时的网站(这是我在本地测试的一个,URL 是 file:///Users/...projectX.html'),听起来像这与问题有关吗?如果是这样,我将编辑我的问题以澄清这一点..
-
这取决于您打开发出此 AJAX 请求的页面的方式。什么是域?是否以
file://开头? -
是的,它确实以 file:// 开头。是不是我无法请求我的 mamp 服务器从网站(在本例中为 theguardian)检索信息并在网站不存在时将其回显?如果是这样,您如何在上线之前测试这些类型的跨域请求?
-
很有可能。您可能希望您的页面通过您的 MAMP 服务器提供服务。假设您的 API 也由 MAMP 提供,来源将是相同的,因此您根本不需要设置任何 CORS 标头。
标签: javascript php curl mamp mamp-pro