ensnare ensnare - 1 year ago 76
AngularJS Question

How can I identify requests made via AJAX in Python's Flask?

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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download