【发布时间】: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