标签:
def is_ajax(self):
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
is_ajax()会判断请求头里 'HTTP_X_REQUESTED_WITH'的值。如果请求方式不为ajax,那么请求头里是不含'HTTP_X_REQUESTED_WITH'的。如果是ajax请求,is_ajax()则会返
回True.
前端代码:
$.ajax({
url: '/app2/host/', // url一定要写成相对路径
type: 'POST',
data: {'choice':del},
complete: function () {
console.log('aaa')
},
success: function (data) {
if(data === 'success')
console.log(data)
}
})
后端代码:
def host(request):
if request.is_ajax():
print('get ajax')
if request.POST.get('choice', None):
choice = request.POST.get('choice')
print(choice)
return HttpResponse('success')
else:
return HttpResponse('failed')
标签:
来源: https://www.cnblogs.com/xyzallstacker/p/9644427.html