简体   繁体   English

如何将ajax响应转换为html

[英]How to convert ajax response to html

I have profile page where user can upload self comments. 我有个人资料页面,用户可以上传自我评论。

Code which upload comments in json 在json上传注释的代码

$('#profile-comment .stream-more-link').click(function (e) {
    e.preventDefault();
    //Increment stream page
    streamPage++;
    //Get post data in json
    $.ajax({
        url : "/profile/comments",
        dataType: "json",
        data: {"page":streamPage},
        success: function(response) {
            if (response.length > 0) {
                $('#commentTmpl').tmpl(response).appendTo('.stream-items');
            } else {
                $('.stream-more').hide();
            }
        }
    })
    return false;
});

jQuery Template jQuery模板

<script type="text/x-jquery-tmpl" id="commentTmpl">
    <div class="stream-item">
        {{if post.length}}
        <div class="article-title">
            <a class="article-title-link" href="${post.titleLink}">${post.title}</a>
        </div>
        {{/if}}
        <div class="comment level-1" id="c${comment.id}">
            <div class="comment-wrap">
                <div class="comment-img">
                    <img src="${user.foto}" width="48px" height="48px" alt="имя профиля" />
                </div>
                <div class="comment-content">
                    <a class="author" href="${user.link}">${user.name}</a>
                    <div class="text">
                        <p>${comment.content}</p>
                    </div>
                    <div class="meta">
                        ${comment.date}
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

JSON response sample from chrome json preview: 来自chrome json预览的JSON响应示例:

8: {post:[],…}
comment: {id:46298, content:<i>проверка</i>, на то как<b> </b><b><i>работает</i></b><b> </b>очистка.<br>,…}
content: "<i>проверка</i>, на то как<b> </b><b><i>работает</i></b><b> </b>очистка.<br>"
date: "20.09.2011 02:22"
id: "46298"
post: []
user: {name:moderator, link:/profile/view/username/moderator,…}
foto: "/files/user/fotos/thumb/default.jpg"
link: "/profile/view/username/moderator"
name: "moderator"
9: {post:[], comment:{id:46303,…}, user:{name:moderator, link:/profile/view/username/moderator,…}}
comment: {id:46303,…}
content: "Содержимое с <b>HTML</b>-<i>разметкой <img src="/files/emoticons/love.gif" alt="love" title="love" /><br></i>"
date: "20.09.2011 02:19"
id: "46303"
post: []
user: {name:moderator, link:/profile/view/username/moderator,…}
foto: "/files/user/fotos/thumb/default.jpg"
link: "/profile/view/username/moderator"
name: "moderator"

And comment content not evaluated into html, just string. 评论内容未评估为html,只是字符串。

If we want output html in template we must using this consturction {{html varName }} 如果我们想在模板中输出html,我们必须使用这个consturction {{html varName}}

More about this 更多关于这个

http://api.jquery.com/template-tag-html/ http://api.jquery.com/template-tag-html/

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

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