request_xhr_key = request.headers.get('X-Requested-With')
if request_xhr_key and request_xhr_key == 'XMLHttpRequest':
#mystuff
return result
abort(404,description="only xhlhttprequest is allowed")
if request.method == 'POST' and request.headers.get('X-Requested-With') == 'XMLHttpRequest':
current_app.logger.info('Recognized an AJAX request!')
# rest of code to handle initial page request
4条答案
按热度按时间cnwbcb6i1#
Flask在
request
对象中带有一个is_xhr
属性。字符串
**注意:**此解决方案已弃用,不再可行。
ntjbwcob2#
对于未来的读者:我做的是下面这样的:
字符串
这将给予一个404错误,如果请求头不包含“XMLHttpRequest”值。
vbopmzt13#
没有任何方法可以确定一个请求是否是由一个代理提出的。
我发现对我有效的方法是简单地为xhr请求包含一个get参数,而在非xhr请求中省略该参数。
举例来说:
example.com/search?q=Boots&api=1
example.com/search?q=Boots
8e2ybdfx4#
我用了这个,它工作得很好:
字符串