如果要獲取當前請求信息,可以導入request模塊
~~~
from flask import request
~~~
獲取get信息
~~~
name=request.args.get('name')
~~~
獲取post信息
~~~
name=request.form.get('name')
~~~
獲取所有請求數據
~~~
request.values
~~~
如果前端要向后端提交Json格式的數據,需要設置content-type參數為application/json,并且將data參數設置為字符串形式。
~~~
<script>
var param={
'name':'chenyy',
'age':18
}
$.ajax({
url:"url",
data:JSON.stringify(param),
type:'POST',
dataType:'json',
contentType:'application/json; charset=UTF-8',
success:function(result){
console.log(result)
},
error:function(e){
console.log(e)
}
});
</script>
~~~
python接收ajax請求數據:
~~~
data=request.get_json()
name=data.get('name')
~~~