require.js is requesting html files but serving them as script elements

我与影子孤独终老i 提交于 2019-12-24 07:14:49

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/11002182/require-js-is-requesting-html-files-but-serving-them-as-script-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!