Home » flask » FLASK json数据不能用表单接收, 表单最好也不要接收json数据

FLASK json数据不能用表单接收, 表单最好也不要接收json数据

json数据不能用表单接收, 表单最好也不要接收json数据
Flask 中接收与发送表单方式
from flask import Flask,render_template,request

@app.route(‘/login’,methods=[‘GET’,’POST’] )
def login():
if request.method == ‘POST’:
username = request.form[‘username’]
password = request.form[‘password’]
print username,password
return render_template(‘login.html’,method=request.method)
1
2
3
4
5
6
7
8
9
Flask 中接收与发送json 方式详解
(转)https://blog.csdn.net/gymaisyl/article/details/86629874

1、利用flask的request.form.get()方法接收

from flask import Flask
from flask import jsonify
from flask import request
import json

@app.route(“/login”, methods=[‘POST’] )
def login():
data_ = request.form.get(‘data’)
data = json.loads(data)
username = data[‘username’]
password = data[‘password’]

return jsonify({“login”: Login.login(username, password)}) # 返回布尔值
1
2
3
4
5
6
7
8
9
10
11
12
13
2、 利用flask的request.get_data()方法接收

from flask import Flask
from flask import jsonify
from flask import request
import json

@app.route(“/flask/login”, methods=[‘POST’] )
def login():
data = request.get_data()
data = json.loads(data)
username = data[‘username’]
password = data[‘password’]

return jsonify({“login”: Login.login(username, password)}) # 返回布尔值
1
2
3
4
5
6
7
8
9
10
11
12
13
3、利用flask的request.get_json()方法接收

from flask import Flask
from flask import jsonify
from flask import request

@app.route(“/flask/login”, methods=[‘POST’] )
def login():
data = request.get_json()
username = data[‘username’]
password = data[‘password’]

return jsonify({“login”: Login.login(username, password)}) # 返回布尔值
————————————————
版权声明:本文为CSDN博主「风华浪浪」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a6864657/article/details/98968747