【问题标题】:RequireJs text plugin gives Cross origin error in ChromeRequireJs 文本插件在 Chrome 中出现跨源错误
【发布时间】:2011-12-06 22:49:40
【问题描述】:

我有一些非常基本的 RequireJs 代码,可以用简单的 html 获取 html 文件。 在 Firefox 8.0 中它工作正常,但是在我构建的 Chrome (17.0.9.xxx) 中我收到以下错误:

仅 HTTP 支持跨源请求。

这可能只是这个版本的 Chrome 或一般文本插件的问题吗?

define([
    'jquery', 
    'backbone', 
    'text!templates/home/listOfStuff.html'
    ], function ($, Backbone, mainTemplate) {
        var mainView = Backbone.View.extend({
            el: $('#list'),
            render: function () { 
                this.el.html(mainTemplate);
            }

        });


        // return the view object
        return new mainView;

});

当 Require 尝试获取 html 文件时发生错误。

【问题讨论】:

  • 我在这里没有看到任何问题,而且我有一个包含多个文本的相当大的项目!插件使用。 Chrome 17.0.943.0(截至今天的 Chrome Canary)
  • 查看您的 chrome 控制台并尝试找到 xhttp 请求。 chrome 试图从哪个 url 获取 html 文件?
  • @ProTom file:///C:/Users/xxx/xxx/Projects/RequireJs/templates/home/listOfStuff.html 这是我试图拉入的html文件的位置.
  • 所以问题是您从文件系统而不是网络服务器运行应用程序。在某些浏览器中,XHR 请求在文件系统中不起作用。您必须启动一些 http 服务器,如 Apache(或 node.js 或其他东西)并通过服务器运行您的应用程序。你的代码应该可以在任何浏览器中运行。
  • 这很奇怪,它可以在 Firefox 中工作,对吧?我找到了另一个例子,它工作正常。但是是的,在某种服务器上运行它是可行的......

标签: backbone.js requirejs


【解决方案1】:

这是 chrome 的本地文件系统访问策略。对于本地开发,您只需添加以下标志:

--allow-file-access-from-files --disable-web-security

【讨论】:

  • 当我添加这个参数--disable-web-security 时,chrome 给了我这个警告'......稳定性和安全性将受到影响'。想知道这是否会以任何方式损害我的系统。
  • 你在哪里/如何添加这些?
  • @natecraft1 您可以使用参数从 cmd 执行 chrome。 link
【解决方案2】:

我同意 ProTom 的观点,如果您设置了一个网络服务器来提供您的 html 页面,那么您应该一切就绪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2015-11-15
    • 2021-08-19
    • 2020-01-21
    相关资源
    最近更新 更多