flask接受jquery ajax傳遞過來的引數
阿新 • • 發佈:2018-12-31
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不能!