gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:41:02 25 4
gpt4 key购买 nike

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

当我使用 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.

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

最佳答案

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.

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

或者

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

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

* 允许所有跨域请求

关于javascript - 如何解决 Firefox 中的跨域请求被阻止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25051759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com