《精选问答》挑选CSDN问答频道每周最新最热的优质回答,为大家提供可信赖的优质解答,点击查看更多已解决问题
目录
1、flask做后端去获取前端vue+axios(post)的数据,flask这一块该如何去做
1、flask做后端去获取前端vue+axios(post)的数据,flask这一块该如何去做
问题描述:
flask做后端去获取前端vue+axios(post)的数据,flask这一块该如何去做
解决方案——来自@CSDN专家-showbo的回答
简单写了个示例:
server.py
from flask import Flask, request,render_template
import json
app = Flask(__name__, static_url_path='')
@app.route('/', methods=['POST'])
def index():
username=request.form.get("username") or "username为空"
username2=request.form.get("username2") or "username2为空"
password=request.form.get("password") or "password为空"
password2=request.form.get("password2") or "password2为空"
#你的处理逻辑,下面只是简单返回提交的数据
return username+username2+password+password2
@app.route('/register', methods=['GET'])
def register():
return render_template("register.html")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
templates\register.html
<meta charset="utf-8" />
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
params = { username: 'username', password: 'pwd1', username2: 'username2', password2: 'pwd2' };
var data = '';
for (attr in params) data += '&' + attr + '=' + encodeURIComponent(params[attr]);//转键值对,然后设置下content-type。axios的content-type是application/json的,不是键值对
data = data.substring(1)
axios.post('/', data,
{ headers: { 'con