【问题标题】:Chrome gives " No 'Access-Control-Allow-Origin' header is present on the requested resource error" but firefox doesn'tChrome 给出“No 'Access-Control-Allow-Origin' header is present on the requested resource error”,但 firefox 没有
【发布时间】:2017-12-01 17:22:27
【问题描述】:

我有这个脚本来获取用于创建 JIRA REST API 问题的字段元数据。这是我的要求:

$.ajax({
                    url:"https://jira.nutanix.com/rest/api/2/issue/createmeta",
                    headers: { 'Access-Control-Allow-Origin': '*' },
                    headers: {'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS'},
                    headers: { 'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token' },
                    type: "GET",
                    success: function (result) {

                        console.log(result);
                    }

                });

当它在 chrome 上运行时,我得到了错误:

XMLHttpRequest cannot load https://jira.nutanix.com/rest/api/2/issue/createmeta. response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

但是,当我在 firefox 上运行相同的脚本时,我没有收到此类错误。我读过关于 CORPS 的文章,我想我明白了。但我不明白为什么它适用于 Firefox,但不适用于 chrome。这是我必须忍受的铬合金吗?

我遇到了一些扩展或更改 chrome 设置以禁用 CORS。但我认为这只能在我正在开发的机器上解决。最终用户呢?我不能指望他们都安装了扩展。

感谢任何帮助。谢谢:)

【问题讨论】:

    标签: jquery ajax google-chrome firefox


    【解决方案1】:

    大多数现代浏览器都会阻止对跨服务器的 ajax 调用。如果您有权访问服务器,则需要在服务器上启用 CORS(跨域资源共享),或者您需要为此找到一些替代方案。

    您可以参考this的帖子。

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 2021-04-28
      • 2016-07-10
      • 1970-01-01
      • 2022-08-08
      • 2013-12-11
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      相关资源
      最近更新 更多