我正在使用koExternalTemplateEngine加载外部模板。
当模板位于同一站点或从同一服务器上的其他站点提供时,此方法可以正常工作。
但是,当我尝试在远程服务器上引用模板时,它不起作用。我得到http 200 ok但状态代码为0(Response中没有任何内容,没有html)。
代码示例如下:
<script src="Content/Scripts/ko/lib/koExternalTemplateEngine_all.js"></script>
<script>infuser.defaults.templateSuffix = ".tmpl.html";
infuser.defaults.templateUrl = "http://www.anotherServer.com/koTemplates";</script>
<div data-bind="template: { name: 'koTemplate1' }"></div>
是否可以在远程服务器上引用模板,如果可以,我缺少什么?
答案 0 :(得分:0)
我建议您沿着使用requireJS及其文本插件的路线来加载外部交叉源内容并在那里配置xhr:
config: {
text: {
useXhr: function (url, protocol, hostname, port) {
return true;
}
}
}
您可以在requirejs config中使用此属性将其设置为使用xhr并激活CORS。
确保这不适用于ie 6和7以及ie 8和9你需要使用:
window.XDomainRequest
完成所有这些后,您可以简单地将外部模板调用为requirejs模块依赖项,如下所示:
define("moduleName", ["ko", "text!templates/surveys.html"],
function(html){
$('body').append(html);
ko.applyBindings(new viewmodel)...
});
一些很好的链接: require.js is requesting html files but serving them as script elements