How to determine if an HTTP request is asynchronous in Python or Pylons

 ̄綄美尐妖づ 提交于 2019-12-11 03:43:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!