2013-03-29 94 views
3

使用require.js文本插件(https://github.com/requirejs/text)從服務器加載我的html模塊。requirejs文本插件將腳本解釋爲腳本

服務器有另一個主機,所以通過在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:

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

有沒有人有任何想法問題出在哪裏?

感謝您的幫助

+1

我發現重寫useXhr方法不起作用。所以一個修改插件自身的功能 –

回答

0

我花了很多時間找出來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' 
}