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

后端 未结 3 2249
無奈伤痛
無奈伤痛 2021-02-19 04:59

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?

3条回答
  •  后悔当初
    2021-02-19 05:26

    There isn't any way to be certain whether a request is made by ajax.

    What I found that worked for me, was to simply include a get parameter for xhr requests and simply omit the parameter on non-xhr requests.

    For example:

    • XHR Request: example.com/search?q=Boots&api=1
    • Other Requests: example.com/search?q=Boots

提交回复
热议问题