Detect ajax vs regular browser post in Python 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')
精彩评论