gpt4 book ai didi

javascript - 是否有任何选项可以让我们检查 AJAX 请求的来源

转载 作者:太空宇宙 更新时间:2023-11-03 17:05:54 25 4
gpt4 key购买 nike

我正在开发一个 Django 项目,其中在 View 中我定义了一个返回查询集的 View 集。我想在 View 集中使用 if else 条件,类似于:

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

我可以寻求帮助吗,因为我无法从其他相关问题和博客获得任何帮助。

编辑:来自 xyz.js 的 AJAX 请求调用 View 集并返回查询集对象。来自 abc.js 的 AJAX 请求使用列表调用 View 集并返回查询集对象。

编辑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

关于javascript - 是否有任何选项可以让我们检查 AJAX 请求的来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com