I've a handler for a URL,
@app.route("/", methods=['POST'])
@crossdomain(origin='*')
def hello():
ss=str(request.data)
print ss
return ss
The handler cannot retrive the data part of the request. When using jQuery:
jQuery.ajax(
{
type: "POST",
dataType: "json",
data:"adasdasd",
url: 'http://127.0.0.1:5000/',
complete: function(xhr, statusText)
{ alert(xhr.responseText) }})
nothing is returned
This worked for me.
In Javascript:
In Python (flask):
NOTE: The points are;
interesting, as it turns out you can only use
request.data
if the data was posted with a mimetype that flask can't handle, otherwise its an empty string""
I think, the docs weren't very clear, I did some tests and that seems to be the case, you can take a look at the console output the flask generates when you run my tests.taken from http://flask.pocoo.org/docs/api/
but since we are doing a standard
POST
using jsonflask
can handle this quite well as such you can access the data from the standardrequest.form
thisss=str(request.form)
should do the trick as I've tested it.As a side note
@crossdomain(origin='*')
this seems dangerous, theres a reason why we don't allow cross site ajax requests, though Im sure you have your reasons.this is the complete code I used for testing:
output:
and my system:
using google chrome
Version 20.0.1132.57
Hmm, I get AJAX requests via
request.form
. I'm using DataTables and specify it like this:The xhr() function is simple:
usersview is an instance of the my Grid() object. In this case, it's only interesting on how it get's to the ajax data that DataTables sent in:
I have been working with similar functionality and after a bit of messing around with the ajax and python, this is what I came up with for python reading the ajax data
JavaScript:
Python: