简体   繁体   English

未捕获的TypeError:$ .tmpl不是函数

[英]Uncaught TypeError: $.tmpl is not a function

I am using JQuery template feature to render template in html page. 我正在使用JQuery模板功能在HTML页面中呈现模板。 I have loaded the libraries using 我已经使用加载了库

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js"></script> 
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>

Now on a Button hit I am calling the function displayProducts() and using ajax calling backend Java Service to get data in JSON format. 现在,在Button命中时,我正在调用函数displayProducts()并使用ajax调用后端Java Service来获取JSON格式的数据。 responseJson is the data that I am getting back from backend. responseJson是我从后端获取的数据。

My Template file name is productList.html. 我的模板文件名为productList.html。 And on success, I am calling the tmpl function of jquery. 成功的话,我正在调用jquery的tmpl函数。 But it is showing the following error. 但是它显示以下错误。

Uncaught TypeError: $.tmpl is not a function 未捕获的TypeError:$ .tmpl不是函数

Kindly suggest what's wrong in my code. 请提出我的代码有什么问题。

function displayProducts() {
    $.ajax({
                url : 'DisplayProducts',
                data : {
                    searchKey : $('#searchText').val()
                },
                success : function(responseJson) {
                    console.log(JSON.stringify(responseJson));
                    $.get('template/productList.html', function(htmlTemplate) {
                        $.tmpl(htmlTemplate, responseJson).appendTo('#container2');
                    });                     
                }
            });
}

EDITED: 编辑:

When I am using 当我使用

var blogPosts = [
             {
                 postTitle: "How to fix a sink plunger in 5 minutes",
                 postEntry: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.",
                 categories: ["HowTo", "Sinks", "Plumbing"]
             },
             {
                 postTitle: "How to remove a broken lightbulb",
                 postEntry: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.",
                 categories: ["HowTo", "Lightbulbs", "Electricity"]
             },
             {
                 postTitle: "New associate website",
                 postEntry: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna."
             }
         ];
         $.get('template/sample.html', function(template) {
             $.tmpl(template, blogPosts).appendTo('#container3');
         });

inside < script > tag. 在<script>标记内。 It is working fine. 一切正常。 But when I am using the way I posted earlier it is showing the error. 但是,当我使用之前发布的方式时,它显示了错误。

just check the 'jquery.tmpl.min.js' reference must be added, here the link file. 只需检查必须添加'jquery.tmpl.min.js'参考,此处为链接文件。 https://github.com/BorisMoore/jquery-tmpl https://github.com/BorisMoore/jquery-tmpl

There was no problem at all with the version number in my code. 我的代码中的版本号根本没有问题。 There was a missing in the html template. html模板中缺少内容。 That's why I was getting the error. 这就是为什么我得到错误。 Fixed it now. 立即修复。

我在项目中修复了此错误,我只是将此链接添加到jQuery资源中:

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM