开发者

Detect ajax vs regular browser post in Python Pylons

开发者 https://www.devze.com 2022-12-29 14:06 出处:网络
How can I detect whether a form was submitted via an AJAX post or just a browser submit开发者_C百科 in Pylons?

How can I detect whether a form was submitted via an AJAX post or just a browser submit开发者_C百科 in Pylons?

For example:

if 'name' in request.POST:
    #Do something

Would be true if 'name' was submitted via ajax or just a regular post. How can I differentiate?

Thank you.


You can use request.is_xhr. This relies on your javascript framework to set the X-Requested-With: XMLHttpRequest header, but that's not bad since most frameworks like jquery add this header.


I check for the accepts json in the request headers

def controller_accepts_json():
    return 'application/json' in request.headers.get('accept', '')


this may help.

import os

os.environ.has_key('HTTP_X_REQUESTED_WITH')
0

精彩评论

暂无评论...
验证码 换一张
取 消