问题
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