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:
var data = {
data: JSON.stringify({
"value":'asdf'
})
}
};
$.ajax({
url:"/",
type: 'POST',
data: data,
success: function(msg){
alert(msg);
}
})
Python:
from flask import json
@app.route("/", methods=['POST'])
def get_data():
data = json.loads(request.form.get('data'))
ss = data['value']
return str(ss)
注意:
javascript
不能如下写法,不知为什么:
$.ajax({
url:"/",
type: 'POST',
data: {"value":"val1"},
success: function(msg){
alert(msg);
}
})
在java和nodejs中可以这么写,但flask不能!