且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决跨源请求在Firefox中阻止错误

更新时间:2022-10-28 08:29:46


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


这意味着你应该有api您的代码中的 url )以及您脚本的文件必须位于同一个域中



在API( url $ c $>)中添加 Access-Control-Allow-Origin头 c>在你的代码中)domain
 < FilesMatch\。(php)$> 
< IfModule mod_headers.c>
Header set Access-Control-Allow-Origin*
>
< / FilesMatch>

* 允许所有跨域请求


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

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

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.

How do I solve this? Please help.

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.

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

Or

Add the Access-Control-Allow-Origin header in the API(url in your code) domain

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

* to allow all cross domainrequests