Ich schreibe diese kleine WSGI Skript:Wie kann man wissen, dass es sich um eine Ajax-Anfrage in einem WSGI-Skript handelt?
def application(environ, start_response):
data = '\n'.join(sorted(['%s = %s' % i for i in environ.items()]))
print data
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
return iter([data])
Ich frage mich, wie kann ich erkennen, ich eine Ajax-Anforderung erhalten: Wenn ich laufe es mit gunicorn und wenn ich erzeugen ein Ajax-Request mit $ Schnipsel() von jQuery, habe ich dies für environ Variable:
HTTP_ACCEPT = */*
HTTP_ACCEPT_ENCODING = gzip, deflate, sdch, br
HTTP_ACCEPT_LANGUAGE = fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
HTTP_ACCESS_CONTROL_REQUEST_HEADERS = 16hhs673uh
HTTP_ACCESS_CONTROL_REQUEST_METHOD = GET
HTTP_CONNECTION = keep-alive
HTTP_HOST = localhost:8222
HTTP_ORIGIN = http://localhost:6543
HTTP_REFERER = http://localhost:6543/test2
HTTP_USER_AGENT = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
PATH_INFO = /test2
QUERY_STRING = name=John4&time=2pm
RAW_URI = /test2?name=John4&time=2pm
REMOTE_ADDR = 127.0.0.1
REMOTE_PORT = 40668
REQUEST_METHOD = OPTIONS
SCRIPT_NAME =
SERVER_NAME = 127.0.0.1
SERVER_PORT = 8222
SERVER_PROTOCOL = HTTP/1.1
SERVER_SOFTWARE = gunicorn/19.7.1
gunicorn.socket = <socket._socketobject object at 0x7f14844e51a0>
wsgi.errors = <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f14844e47d0>
wsgi.file_wrapper = <class 'gunicorn.http.wsgi.FileWrapper'>
wsgi.input = <gunicorn.http.body.Body object at 0x7f14844e4950>
wsgi.multiprocess = False
wsgi.multithread = False
wsgi.run_once = False
wsgi.url_scheme = http
wsgi.version = (1, 0)
ich sehe keine Informationen wie HTTP_X_REQUESTED_WITH, so wie Django oder Pyramide kann bekannt es eine ajax-Anforderung ist?
Warum müssen Sie das auf der Serverseite wissen? Warum sollte es Sie interessieren, ob der Client auf eine Antwort wartet oder sie asynchron behandelt (oder welche Sprache sie verwendet)? – UnholySheep
Ich möchte das aus Sicherheitsgründen überprüfen. – Eric
Ich bin nicht wirklich sicher, welche Sicherheitsgründe das sein könnten, aber AFAIK können Sie es nicht sagen (da der Kunde keine Verpflichtung hat, solche Informationen zu senden) - ich denke, der 'HTTP_USER_AGENT' ist der nächste, den Sie wissen können wurde von einem Browser gesendet – UnholySheep