3

Im using the require.js text plugin (https://github.com/requirejs/text) to load my html modules from the server.

The server has an other host so a allow coss-domain-request by useing Xhr in the require.config object.

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

    return true;
  }
},

But when Im load some module the Browser try to interpret the loaded file as javascript file.

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

in the broser a get:

Resource interpreted as Script but transferred with MIME type text/html: "http://app-id.appspot.com/gadget/js/app/view_templates/header.html". require.js:1843 Uncaught SyntaxError: Unexpected token < header.html:1

Did anyone have any idea where the problem is ?

Thanks for help

Stefan B.
  • 374
  • 3
  • 13
  • 1
    I found out that overriding the useXhr method don't work. So a modify the function in the plugin self – Stefan B. Mar 31 '13 at 00:15

1 Answers1

0

Took me many hours to find it out here. useXhr doesn't get called probably because config key is not correct. It is not just text, it requires containing the path as well. So it should be:

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

Or what should also work:

text: {
  useXhr: function (url, protocol, hostname, port) {
    return true;
  }
},
paths: {
    text: 'some/path/to/text'
}
Community
  • 1
  • 1
robsch
  • 9,358
  • 9
  • 63
  • 104