如何解决跨源请求在Firefox中阻止错误 [英] How to solve Cross-Origin Request Blocked error in firefox

查看:120
本文介绍了如何解决跨源请求在Firefox中阻止错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我使用 getJson 方法获取来自REST API的数据,我收到错误:
$ b


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


如何解决这个问题?请帮助。

解决方案


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


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



在API( url )中添加 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

这篇关于如何解决跨源请求在Firefox中阻止错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆