問題描述
有沒有辦法在 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!