简体   繁体   English

使用单个 AJAX 调用加载两个 div

[英]Load two divs with a single AJAX call

I'm trying to use load() to load two divs #follow-x and #follow-y ajaxly with a click of a button.This is what I have tried in success function,but doesn't work,but works if I remove one of the function,so it loads only one div but I want it to load both.Thanks in advance我正在尝试使用 load() 通过单击按钮加载两个 div #follow-x#follow-y ajaxly。这是我在success功能中尝试过的,但不起作用,但如果我可以删除一个函数,所以它只加载一个 div,但我希望它同时加载。提前致谢

$('#follow').click(function(){

          $.ajax({

                   type: "POST",
                   url: "{% url 'follow_class' %}",
                   data: {'pk': '{{class.pk}}' , 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                   dataType: "json",
                   success: function(){
                       $('#follow-x').load("{% url 'class_details' %} #follow-x");}
                       function(){
                       $('#follow-y').load("{% url 'class_details' %} #follow-y");}

              }); 
        });
</script>

Dont mind the tags {} I'm using Django不要介意标签{}我正在使用 Django

only the first function is being called on success只有第一个函数被调用的success

put both statements in one function将两个语句放在一个函数中

$('#follow').click(function(){

          $.ajax({

                   type: "POST",
                   url: "{% url 'follow_class' %}",
                   data: {'pk': '{{class.pk}}' , 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                   dataType: "json",
                   success: function(){
                       $('#follow-x').load("{% url 'class_details' %} #follow-x");                        
                       $('#follow-y').load("{% url 'class_details' %} #follow-y");
                    }

              }); 
        });

Your second load call is in another function that never gets called您的第二个加载调用位于另一个从未被调用的函数

$('#follow').click(function(){

          $.ajax({

                   type: "POST",
                   url: "{% url 'follow_class' %}",
                   data: {'pk': '{{class.pk}}' , 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                   dataType: "json",
                   success: function(){
                       $('#follow-x').load("{% url 'class_details' %} #follow-x");
                       $('#follow-y').load("{% url 'class_details' %} #follow-y");
                   }

              }); 
        });
</script>

Success is a property of the object passed to AJAX. Success 是传递给 AJAX 的对象的属性。 In your code you attempt to assign it to a function containing the follow-x code, however a syntax error is produced due to the next function statement that violates the obj structure.在您的代码中,您尝试将其分配给包含follow-x代码的函数,但是由于下一个违反 obj 结构的函数语句而产生语法错误。 Try this.尝试这个。

success: function(){
     $('#follow-x').load("{% url 'class_details' %} #follow-x");
     $('#follow-y').load("{% url 'class_details' %} #follow-y");
}

Also consider using the done callback instead of success.还可以考虑使用 done 回调而不是成功。 More info on that here what is difference between success and .done() method of $.ajax关于这里的更多信息, 成功和 $.ajax 的 .done() 方法有什么区别

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

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