Javascript 2个ajax调用同时导致视图中出现问题

Javascript 2个ajax调用同时导致视图中出现问题,javascript,jquery,python,ajax,django,Javascript,Jquery,Python,Ajax,Django,我使用ajax呈现两个查询集(Post和Comment)。因此,当我单击more posts或more comments时,它将加载相应查询集中的下一批对象。下面是显示以下内容的JS函数: $('body').on('click', '.endless_more', function() { console.log($(this).html()); var user_queryset; if ($(this).html() === 'more posts') {

我使用ajax呈现两个查询集(
Post
Comment
)。因此,当我单击
more posts
more comments
时,它将加载相应查询集中的下一批对象。下面是显示以下内容的JS函数:

$('body').on('click', '.endless_more', function() {
    console.log($(this).html());
    var user_queryset;
    if ($(this).html() === 'more posts') {
        console.log('POSTS');
        user_queryset = 'user_posts'
    } else if ($(this).html() === 'more user comments') {
        user_queryset = 'user_comments';
        console.log('COMMENTS');
    } else {
        console.log('none');
    }
    $.ajax({
        type: 'GET',
        url: window.location.href,
        data: {
            'user_queryset': user_queryset
        }

    })
});
下面是从函数接收数据的视图(
user\u posts
user\u comments
,具体取决于单击的内容)

现在,JS工作正常-它根据单击的内容发送正确的数据。因此,视图中的
page\u模板
在ajax调用期间成功更新,但是它呈现父模板(
profile.html
),而不是所选的子模板(
user\u posts.html
user\u comments.html
)。这可能是因为同时发送了两个ajax调用—一个来自
django el pagination
,另一个来自my JS函数。因为我的打印报表,我知道有两个电话。首先,当我第一次加载整个页面时,这里是我的打印语句:

LOAD
Template: profile/profile.html
这是正确的-它加载父模板

现在,当我点击更多帖子时,这里是我的打印声明:

LOAD
Requests: <QueryDict: {'querystring_key': ['page'], 'page': ['2']}>
Queryset: None
Template: profile/profile.html
LOAD
Requests: <QueryDict: {'user_queryset': ['user_posts']}>
Queryset: user_posts
Template: profile/user_posts.html
加载
请求:
Queryset:没有
模板:profile/profile.html
负载
请求:
Queryset:user_posts
模板:profile/user_posts.html

正如你所看到的,它两次穿过我的视野。为什么会这样?第一次获取分页数据时,下一次获取单击按钮时(在本例中为
user\u posts
)。尽管它被调用了两次,但第二次调用有正确的模板要加载(
user\u posts.html
),因此它应该仍然可以工作-但是它不能,它仍然加载父模板。有没有人知道为什么会发生这种情况,以及我如何解决它?我不认为实际的代码有任何错误;如果在我的
request.is\u ajax()
中将其更改为
user\u queryset='user\u posts'
,则它工作正常-仅加载
user\u posts.html
。然而,它似乎无法处理两个查询集

旁注:要验证执行了多少XHR以及执行了哪个XHR,您应该首先检查浏览器开发工具(网络选项卡)。检查服务器端应该是第二步。下面是所有XHR请求的图像(正如预期的那样,我问题中的两个ajax请求位于顶部):-这说明了什么吗?旁注:要验证执行了多少XHR以及执行了哪些XHR,您应该先检查浏览器开发工具(网络选项卡)。检查服务器端应该是第二步。下面是所有XHR请求的图像(正如预期的那样,我问题中的两个ajax请求位于顶部):-这告诉了您什么吗?
LOAD
Requests: <QueryDict: {'querystring_key': ['page'], 'page': ['2']}>
Queryset: None
Template: profile/profile.html
LOAD
Requests: <QueryDict: {'user_queryset': ['user_posts']}>
Queryset: user_posts
Template: profile/user_posts.html