如何识别通过AJAX的Python的烧瓶提出的要求? [英] How can I identify requests made via AJAX in Python's Flask?

查看:136
本文介绍了如何识别通过AJAX的Python的烧瓶提出的要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测,如果浏览器发出通过AJAX(AngularJS)的要求,这样我可以返回一个JSON数组,或者如果我要渲染的模板。我怎样才能做到这一点?

I'd like to detect if the browser made a request via AJAX (AngularJS) so that I can return a JSON array, or if I have to render the template. How can I do this?

推荐答案

瓶带有请求对象is_xhr属性。

Flask comes with a is_xhr attribute in the request object.

from flask import request
@app.route('/', methods=['GET', 'POST'])
def home_page():
    if request.is_xhr:
        context = controllers.get_default_context()
        return render_template('home.html', **context)

这篇关于如何识别通过AJAX的Python的烧瓶提出的要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆