简体   繁体   English

无法使用Ajax添加评论

[英]Can't add comment with ajax

I'm trying to add comment using ajax. 我正在尝试使用ajax添加评论。 Here is my ajax code: 这是我的ajax代码:

$(document).ready(function(){
    $("#add_comment").click(function(event){
        event.preventDefault();
        var article_id = {{article.id}};
        $.ajax({
            url: "http://127.0.0.1:8000/articles/addcomment/" + article_id + '/',
            type: "get",
            success: function(result){

                        if(result === "validation error"){
                            alert(result);
                        }else{
                            var data = JSON.parse(result);
                            $("#comment_block").append("<div class = "comment"><span>" + data['owner'] + "</span><p>" + data['comment_text'] + "</p></div>");
                        }
                    }
            });
    });

});

Here is my form of adding comment in django template: 这是我在Django模板中添加评论的形式:

</div>
    {% if username %}
    <form action="/articles/addcomment/{{ article.id }}/" method="post" id = "comment_form">
    {% csrf_token %}
    {{ form }}
    <input type="submit" class="button"  value="Добавить комментарий" id = "add_comment">
    </form>
    {% endif %}

 </div>

Trying to debug, I noticed that it doesn't even step into ajax body. 在尝试调试时,我注意到它甚至没有进入ajax主体。 What am I doing wrong then? 那我在做什么错呢? I've done ajax query with counting likes and had success in that. 我已经完成了用喜欢计数的ajax查询,并获得了成功。

Change to the following: 更改为以下内容:

JS Code: JS代码:

$(document).ready(function(){
$("#comment_form").submit(function(event){
    event.preventDefault();
    var article_id = $(this).find('.article_id').value();
    $.ajax({
        url: "http://127.0.0.1:8000/articles/addcomment/" + article_id + '/',
        type: "post",
        success: function(result){

                    if(result === "validation error"){
                        alert(result);
                    }else{
                        var data = JSON.parse(result);
                        $("#comment_block").append("<div class = 'comment'><span>" + data['owner'] + "</span><p>" + data['comment_text'] + "</p></div>");
                    }
                }
        });
    return false;
});

}); });

Django Template: Django模板:

</div>
{% if username %}
<form action="/articles/addcomment/{{ article.id }}/" method="post" id = "comment_form">
{% csrf_token %}
{{ form }}
<input type="hidden" class="article_id" value="{{ article.id }}" />
<input type="submit" class="button"  value="Добавить комментарий" id = "add_comment">
</form>
{% endif %}

 </div>

Try this 尝试这个

HTML 的HTML

</div>
    {% if username %}
    <form action="#" method="post" id = "comment_form">
    {% csrf_token %}
    {{ form }}
    <input type="text" name = "article" value="{{ article.id }}" id = "article_id">
    <input type="text" name = "comment" value="comment goes here" id = "comment_text">
    <input type="submit" class="button"  value="???????? ???????????" id = "add_comment">
    </form>
    {% endif %}

 </div>

jQuery jQuery的

  $(document).ready(function(){
    $("#add_comment").click(function(event){
        event.preventDefault();
        var article_id = $('#article_id').val();
        $.ajax({
            url: "http://127.0.0.1:8000/articles/addcomment/" + article_id + '/',
            type: "POST", /*i belive you are posting data to BE api*/
            data:{'comment':"your comment goes here"}, /* use this get what given in input  $("#comment_text").val()*/
            success: function(result){
                        /*process data after success call*/
                    }
            });
    });

});

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

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