问题
I can't help but think that this is a duplicate, but if it is, I can't seem to find the doppelganger.
I'm just starting to learn Python (focusing on Pylons) and I'd like to know if there is a way to determine if a call to a controller is done asynchronously or not. In PHP it would look something like this:
function isAjax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}
Is there one that works well for all of Python, or is there perhaps one that works very well only in Pylons?
Thanks in advance!
回答1:
To directly translate that code to python using pylons you would do something along the lines of:
def isAjax(request):
return request.environ.get('HTTP_X_REQUEST_WITH') == 'XMLHttpRequest'
where request is the request object passed to the controller.
回答2:
Paste supports this method:
request.is_xhr
It's implemented as:
return self.environ.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest'
来源:https://stackoverflow.com/questions/4139201/how-to-determine-if-an-http-request-is-asynchronous-in-python-or-pylons