Loading Handlebars template via AJAX returns docum

2019-08-14 12:48发布

I'm loading this Handlebars.js template:

<ul></ul>

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] 

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.

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

1条回答
啃猪蹄的小仙女
2楼-- · 2019-08-14 13:06

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
});
查看更多
登录 后发表回答