检查请求是否是 Python 中的 AJAX [英] Check if request is AJAX in Python

查看:27
本文介绍了检查请求是否是 Python 中的 AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Python 中检查请求是否为 AJAX?

Is there a way to check if a request is AJAX in Python?

相当于 PHP 的 $_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest'?

The equivalent of PHP's $_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest'?

推荐答案

如果 AJAX 框架在其请求中设置 X-Requested-With 标头,那么您将能够使用该标头来检测 AJAX 调用.这取决于客户端框架.

If the AJAX framework sets the X-Requested-With header in its requests, then you will be able to use that header to detect AJAX calls. It's up to the client-side framework to do this.

获取 HTTP 标头取决于您选择的 Python 框架.在 Django 中,request 对象有一个 is_ajax方法可以直接使用.

Getting hold of the HTTP headers depends on your Python framework of choice. In Django, the request object has an is_ajax method you can use directly.

这篇关于检查请求是否是 Python 中的 AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆