javascript 如何解决Firefox中的跨域请求阻止错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25051759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:48:15  来源:igfitidea点击:

How to solve Cross-Origin Request Blocked error in firefox

javascriptjqueryhtml

提问by user2806784

<script>
    $.getJSON('url', function (data) {
        console.log("Before:"+data);
        t = data;
        console.log("After:"+t);
    });
</script>

When I am using getJsonmethod to get data from REST API , I am getting the error:

当我使用getJson方法从 REST API 获取数据时,出现错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at the url. This can be fixed by moving the resource to the same domain or enabling CORS.

跨域请求被阻止:同源策略不允许读取 url 上的远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。

How do I solve this? Please help.

我该如何解决这个问题?请帮忙。

回答by vimal1083

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at the url. This can be fixed by moving the resource to the same domain or enabling CORS.

跨域请求被阻止:同源策略不允许读取 url 上的远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。

It means you should have api (urlin your code) and the file which has your script must be in same domain

这意味着您应该有 api(url在您的代码中)并且包含您的脚本的文件必须在同一个域中

Or

或者

Add the Access-Control-Allow-Origin headerin the API(urlin your code) domain

添加Access-Control-Allow-Origin header在API中(url在你的代码)域

<FilesMatch "\.(php)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

*to allow all cross domainrequests

*允许所有跨域请求

回答by absolom Mugwagwa

<FilesMatch "\.(php)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

I added this to my httpd-vhosts.conf and the error was solved

我将此添加到我的 httpd-vhosts.conf 中,错误已解决