Gur Dotan
Gur Dotan

Reputation: 922

Loading Handlebars template via AJAX returns document object instead of string

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?

Upvotes: 1

Views: 4677

Answers (1)

Shinichi Goh
Shinichi Goh

Reputation: 26

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
});

Upvotes: 1

Related Questions