简体   繁体   English

有什么办法可以检查AJAX请求的来源

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

I am working on a django project where in views i have defined a viewset which returns a queryset. 我正在django项目中,其中在视图中我定义了一个返回查询集的视图集。 I want to use if else condition in the viewset similar to: 我想在视图集中使用if else条件,类似于:

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

May i please get help as i am not able to get any help from other related questions and blogs. 我无法获得其他相关问题和博客的任何帮助,请问我能获得帮助吗?

EDIT: AJAX request from xyz.js calls the viewset and returns a queryset object. 编辑:来自xyz.js的AJAX请求调用了视图集并返回了一个查询集对象。 AJAX request from abc.js calls the viewset with a list and returns a queryset object. 来自abc.js的AJAX请求使用列表调用视图集,并返回一个queryset对象。

EDIT2: EDIT2:

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"

your ajax 你的ajax

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

and in your view: 并且在您看来:

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

您可以将源信息发布到$ .ajax函数的data属性上。

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

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