我已经研究这个问题大约两天了,但没有找到解决方案。 我也不知道为什么这会如此困难。 我正在尝试从另一个站点捕获数据到本地主机。 它不允许我做任何我想做的事。 我搜索了2天,将apache header修改为 header('访问控制允许来源:*'); 在 PHP 中。也在 .htaccess 中。 我在堆栈和其他网站上研究了其他答案,完全没有任何变化。
这是我的 default.js 文件:
$.ajax({
type: "GET",
url: "http://anothergames.com",
dataType: 'json',
cache: false,
success: function(data)
{
console.log('hello world!');
}
});
这是index.php:
<?php
header('Access-Control-Allow-Origin: *');
echo '<!doctype html>
<html>
<head>
<title>Marrja e te dhenave per udhetimet turistike nga website nepermjet Ajax</title>
<link rel = "stylesheet" href = "default.css">
<meta content="text/html; charset=UTF-8; X-Content-Type-Options=nosniff" http-equiv="Content-Type" />
</head>
<body>
Hello World!
<script src = "js/jquery.min.js"></script>
<script src = "js/default.js"></script>
</body>
</html>';
还将 localhost 更改为其他内容。依然没有。 尝试了其他解决方案,但出现了新问题,mime 类型不匹配和强制 mime 类型。
如果您有任何想法,请告诉我。 我在 Windows 10 和 Chrome 中使用 xampp。 这是我得到的错误。
最佳答案
chrome 不支持 localhost 的 CORS 请求,这里有一个指向该错误的链接: https://bugs.chromium.org/p/chromium/issues/detail?id=67743
你应该做的是使用代理,如下所示:
关于javascript - 无论我尝试什么,请求的资源错误上均不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563687/