2013-05-01 31 views
1

我正在使用koExternalTemplateEngine加载外部模板。远程服务器上的外部模板(通过http)

当模板位于同一站点或从同一服务器上的其他站点提供时,此工作正常。

但是,当我尝试引用远程服务器上的模板时,它不起作用。我得到http 200确定,但状态码为0(响应中没有任何内容并且没有html)。

A码例子如下:

<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

这是'$ .ajax' /浏览器的限制,你不能把请求发送到从JavaScript不同的服务器:看http://stackoverflow.com/questions/1201429/jquery-ajax-fails- when-url-is-from-different-server – nemesv 2013-05-01 07:53:03

+0

啊,很酷,谢谢。 – 2013-05-01 08:39:23

回答

0

我建议你去使用requireJS的路线和加载外部交叉原属含量最高的文字插件和配置XHR使用有:

config: { 
     text: { 
      useXhr: function (url, protocol, hostname, port) { 
       return true; 
      } 
     } 
    } 

你可以使用这个属性在requirejs配置进行设置使用xhr并激活CORS。

确保在IE 6和IE 7和IE 8和9,这并不工作,你需要使用:

window.XDomainRequest 

一旦你做了这一切,你可以simlpy打电话给你的外部模板作为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://github.com/requirejs/text

Configuring xdr for IE

+0

它看起来像我要在同一台服务器上托管模板,现在解决了问题。不过,我会在未来记住这一点,谢谢。 – 2013-05-01 13:47:17

相关问题