Loading Handlebars template via AJAX returns document object instead of string

好久不见. 提交于 2019-12-24 14:24:53

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!