Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript load()函数不使用';I don’我不回最新的邮件_Javascript_Jquery_Python_Html_Django - Fatal编程技术网

Javascript load()函数不使用';I don’我不回最新的邮件

Javascript load()函数不使用';I don’我不回最新的邮件,javascript,jquery,python,html,django,Javascript,Jquery,Python,Html,Django,上面的代码运行良好。但是,“/comments/”url的处理程序不会返回最新的帖子。以下是视图: $("#comment-post-button").click(function(){ var event_id = document.getElementById('event-id').value; var url= '/post-comments/'+event_id +'/'; $.post(url , {csrfmiddlewaretoken: document

上面的代码运行良好。但是,“/comments/”url的处理程序不会返回最新的帖子。以下是视图:

$("#comment-post-button").click(function(){ 
    var event_id = document.getElementById('event-id').value;
    var url= '/post-comments/'+event_id +'/';
    $.post(url , {csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
      content:document.getElementsByName('comment-post-content')[0].value
    }); 
    $("#refresh-comments").load("/comments/" + event_id + '/', null); 
    $("#comment-post-content").val("");  
    return false;
  });
我正在加载一篇成功的文章后要显示的对象,但它没有得到最新的添加。 这是“comments.html”:

def comments(request, event_id):
  if request.method == 'GET':
    event = get_object_or_404(Event, id= event_id)
    variables = RequestContext(request, {
      'event' : event
    })
    return render_to_response('comments.html', variables)
{%用于event.comment\u set.all%}
{{comment.content}}
{{comment.pub_date} {%endfor%}

请帮忙。我被卡住了。

问题在于异步回调。换句话说,
load()
不会等待
post()
完成。因此,将
load
对应的代码移动到回调中。另外,请注意,
comment post content
的重置也是来自
load
方法的回调

{% for comment in event.comment_set.all %}
    <a href="/{{ name }}/" class="first_name">{{ comment.author.first_name }}</a>
    {{ comment.content }}<br>
    {{ comment.pub_date }}
{% endfor %}
另外,请一致使用jQuery。通过这种方式,它变得更具可读性

var data = {csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
  content:document.getElementsByName('comment-post-content')[0].value
}
$.post(url , data, function(){
    $("#refresh-comments").load("/comments/" + event_id + '/', function(){
        $("#comment-post-content").val(""); 
    });
});