通过AJAX加载把手模板返回字符串的文件对象,而不是 [英] Loading Handlebars template via AJAX returns document object instead of string

查看:161
本文介绍了通过AJAX加载把手模板返回字符串的文件对象,而不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载这个Handlebars.js模板:

I'm loading this Handlebars.js template:


<ul></ul>

使用AJAX使用以下code

with AJAX using the following code

$.ajax({
    url : 'collection.handlebars',
    success : function (data) {
        Handlebars.templates["collection"] = Handlebars.compile(data);
    },
    async : false
});

模板编译失败,并在浏览器控制台以下消息:

The template compilation fails with the following message in the browser console:

Uncaught Error: You must pass a string to Handlebars.compile. You passed [object Document] 

调试后,我注意到数据返回的成功回调是一个HTML文档,而不是一个字符串。不过,如果我的模板更改为:

After debugging I noticed that the data returned in the success callback is an HTML document and not a string. However, if I change the template to:


<ul></ul> &nbsp

数据在成功的回调收到一个字符串,一切正常。

the data in the success callback is received as a string and everything works.

我用把手1.0 RC2和Chrome 24。有什么建议?

I'm using Handlebars 1.0 RC2 and Chrome 24. Any suggestions?

推荐答案

您需要指定的数据类型为AJAX:

You need to specify the datatype for the ajax:

$.ajax({
    url : 'collection.handlebars',
    success : function (data) {
        Handlebars.templates["collection"] = Handlebars.compile(data);
    },
    dataType: "text",
    async : false
});

这篇关于通过AJAX加载把手模板返回字符串的文件对象,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆