有没有任何选项可以让我们检查AJAX请求的来源

Is there any option where we can check the source of the AJAX request

本文关键字:请求 AJAX 检查 我们 选项 任何 有没有      更新时间:2023-09-26

我正在处理一个django项目,在该项目中,我在视图中定义了一个返回查询集的视图集。我想在视图集中使用if-else条件,类似于:

if AJAXrequest from xyz.js
{
//do this
}
else if AJAXrequest from abc.js
{
//do that.
}

我无法从其他相关的问题和博客中获得任何帮助,请寻求帮助。

编辑:来自xyz.js的AJAX请求调用视图集并返回一个queryset对象。来自abc.js的AJAX请求调用带有列表的视图集,并返回一个queryset对象。

第2版:

class UserViewSet(ReadOnlyModelViewSet):
if request.is_ajax(): 
    if 'user.js' in request.GET.get('users', ''):
        print " called from user.js"
else:
    print "called from info.js"

您的ajax

$.ajax({
   url: '/yoururl/',
   type: 'get',
   data: {'js': 'abc.js'}
})

在您看来:

def yourview(request):
    if request.is_ajax(): 
        if 'abc.js' in request.GET.get('js', ''):
            # do this
        else:
            # do that

您可以在$.ajax函数的数据属性上发布源信息。