【问题标题】:requirejs text plugin interpret html as scriptrequirejs 文本插件将 html 解释为脚本
【发布时间】:2013-03-20 15:52:26
【问题描述】:

我正在使用 require.js 文本插件 (https://github.com/requirejs/text) 从服务器加载我的 html 模块。

服务器有另一个主机,因此通过在 require.config 对象中使用 Xhr 来允许 coss-domain-request。

text: {
  useXhr: function (url, protocol, hostname, port) {
    // allow cross-domain requests
    // remote server allows CORS

    return true;
  }
},

但是当我加载某个模块时,浏览器会尝试将加载的文件解释为 javascript 文件。

define([
  'text!/view_templates/header.html'], function(html){
    console.log(html) 
})

在broser一个get:

资源解释为脚本但以 MIME 类型传输 文本/html: “http://app-id.appspot.com/gadget/js/app/view_templates/header.html”。 require.js:1843 Uncaught SyntaxError: Unexpected token

有人知道问题出在哪里吗?

感谢帮助

【问题讨论】:

  • 我发现覆盖 useXhr 方法不起作用。所以在插件self中修改函数

标签: javascript cross-domain requirejs requirejs-text


【解决方案1】:

我花了好几个小时才找到它here。 useXhr 没有被调用可能是因为配置键不正确。不仅仅是text,它还需要包含路径。所以应该是:

'some/path/to/text': {
  useXhr: function (url, protocol, hostname, port) {
    return true;
  }
},

或者什么也应该起作用:

text: {
  useXhr: function (url, protocol, hostname, port) {
    return true;
  }
},
paths: {
    text: 'some/path/to/text'
}

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2023-04-09
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多