我正在使用koExternalTemplateEngine加载外部模板。
当模板位于同一站点或从同一服务器上的另一站点提供服务时,这种方法工作得很好。
但是,当我尝试引用远程服务器上的模板时,它不起作用。我得到http 200ok,但状态码为0(响应中没有任何内容,也没有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>可以引用远程服务器上的模板吗?如果可以,我遗漏了什么?
发布于 2013-05-01 17:24:53
我建议你使用requireJS和它的文本插件来加载外部跨源内容,并在那里配置xhr的使用:
config: {
text: {
useXhr: function (url, protocol, hostname, port) {
return true;
}
}
}您可以在要求的配置中使用此属性来将其设置为使用xhr并激活CORS。
确保这在ie6和ie7中不起作用,对于ie8和ie9,你需要使用:
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
https://stackoverflow.com/questions/16314382
复制相似问题