【问题标题】:Why is CORS policy blocking me from loading in HTML snippets from the same folder? [duplicate]为什么 CORS 策略阻止我从同一文件夹加载 HTML 片段? [复制]
【发布时间】:2020-11-05 11:21:54
【问题描述】:

我正在尝试使用查询 load() 方法将 html 从其他文件加载到 div 中。

  $(document).ready(function(){
      $("#president").click(function(){
      event.preventDefault();
      $('#content').load('president.html')
    });
    });

在这里,我想在按下按钮时将 President.html 加载到我的内容 div 中。内容 div 如下所示:

 <div class="col-md-7" id = "content">
    </div>

当我尝试单击按钮时,我在控制台中收到此错误:

CORS 策略已阻止从源“null”访问“file:///UsersKunal/testing/president.html”处的 XMLHttpRequest:跨源请求仅支持协议方案:http、data、chrome、chrome -扩展,https。

两个文件都在同一个文件夹中。

【问题讨论】:

  • file:// 是与允许的协议不同的协议。一个问题:你是在本地运行那个页面吗?

标签: jquery cors


【解决方案1】:

您需要使用XAMPP on windowsmacOS Server on macOS 在您的计算机上设置localhost 服务器(或者,如果您想要更多控制,Homebrew 也可以)。

原因是file:// 不是一个有效的协议,将被所有浏览器阻止,因为当前的 CORS 阻止了以前的安全问题。

请参阅我引用的上述站点上的文档,了解如何设置适当的测试环境,这将解决您的问题。

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 2021-02-22
    • 2018-12-10
    • 2017-10-12
    • 1970-01-01
    • 2021-01-31
    • 2020-05-08
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多