require.js正在请求html文件,但是将它们作为脚本元素 [英] require.js is requesting html files but serving them as script elements

查看:552
本文介绍了require.js正在请求html文件,但是将它们作为脚本元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用require.js文本插件与文本! syntaxt将xhr响应呈现为html。

  text:'libs / require / text'... 



在我看来,我将它们定义为

  define([
'text!templates / categories.html'
,'models / Categories'
,'views / CategoryView'
],...

一切都应该在本地进行。



但是,当我将这个代码推送到服务器时,它开始将* .html作为< script>< / script> 元素加载。



此外,它还将.js扩展名附加到.html文件名。



任何想法?正在加载的资产是CORS 。

解决方案

通过强制文本!插件配置为ALWAY执行CORS来修复此问题。



为此,我添加了以下内容:

  require.config({
text: {
useXhr:function(u,p,h,p){
return true;
}
},...

我猜这是一个问题与CORS是正确的。直到我把它缩小到文本插件,我看到它是尝试加载资产本地。



我也可以通过执行以下操作重新处理这个问题。

  define([
'text!http://location.com/to/external/domain/aka/ cors / templates / categories.html'
,'models / Categories'
,'views / CategoryView'
],...
pre>

其中我明确告诉require和文本插件从远方加载这些文件。



Erik


I'm using the require.js text plugin with the text! syntaxt to render xhr responses as html.

text       : 'libs/require/text' ...

In my views I have defined them as

define([
    'text!templates/categories.html'
    , 'models/Categories'
    , 'views/CategoryView'
    ], ...

Everything works as it should locally.

However when I pushed this code to a server it started loading the *.html as <script></script> elements.

Further more, it was attaching a .js extension to the .html file name as well.

ANy thoughts? The assets being loaded are CORS.

解决方案

Fixed the issue by forcing the text! plugin to configure as ALWAY perform CORS.

To do that I added the following:

 require.config({
     text: {
          useXhr: function (u,p,h,p) {
               return true;
           }
      }, ...

My guess to it being an issue with CORS was correct. It wasn't until I narrowed it down to the text plugin did I see it was trying load assets local.

I also could have rememdied this issue by doing the following.

define([
'text!http://location.com/to/external/domain/aka/cors/templates/categories.html'
, 'models/Categories'
, 'views/CategoryView'
], ...

where I explicitly tell require and the text plugin to load these files from afar.

Erik

这篇关于require.js正在请求html文件,但是将它们作为脚本元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆