问题
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>  
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:
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
});
来源:https://stackoverflow.com/questions/14733756/loading-handlebars-template-via-ajax-returns-document-object-instead-of-string