我发誓我已经复制和粘贴了100种不同的东西,它们应该可以工作,但都不能工作。
我有我的主html文档,test.html:
<html>
<head>
</head>
<body>
<header></header>
<div class='content'>some content</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js' type='text/javascript'></script>
<script src="script.js"></script>
</body>
</html>
另一个html文档test2.html:
<p>this should show up if it works</p>
和一个javascript文档script.js:
$(function() {
$("header").load("test2.html");
});
我的最终目标是获得一个可以被多个不同的html文档通过jquery引用的navbar.html文档。在这里,我试着做一个非常简单的测试来让.load()函数工作。
当我加载test.html时,我希望看到来自test.html和test2.html的文本,但是我没有看到来自test2.html的文本。我在这里做错了什么?
发布于 2020-11-20 00:17:09
对于这种类型的请求,您需要在服务器上运行,因为如果直接从您的html文件运行文件,您将获得CORS (Cross Origin Ressource Sharing)。您可以在Visual Studio代码上使用Live Server,也可以直接在apache服务器上运行perticular代码。
如果我的第一个答案不起作用,你也可以看到这个链接https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9
https://stackoverflow.com/questions/64921778
复制相似问题