1. 微信小程序POST传递数据给flask服务器
import { request } from "../../request/index.js"async handleDetectionPoints() {let params = {url: "/detect_points",data: {"points": arr,"img_name": this.canvasConfig.imgName},header: {},method: "POST"}let res = await request(params)console.log(res)}
let ajaxTimes = 0export const baseURL = "http://192.168.0.106:5000";export const request = params => {let header = { ...params.header}ajaxTimes++;wx.showLoading({title: '加载中',mask: true})params.url = baseURL + params.urlreturn new Promise((resolve, reject) => {wx.request({...params,header,success: result => {resolve(result)},fail: err => {reject(err)},complete: () => {ajaxTimes--if (ajaxTimes == 0) {wx.hideLoading()}}})})
}
from falsk import Flask, requestapp = Flask(__name__)
@app.route("/detect_points", methods=["POST"])
def detect_points():print(request.json.get("points"))print(request.json.get("img_name"))return {"msg": "ok"}if __name__ == "__main__":app.run(host="192.168.0.106", port=5000)
2. python发送post数据给flask服务器
- 拿上面的flask做为代理转发POST请求(JSON格式发送)
import json
@app.route("/detect_points", methods=["POST"])
def detect_points():payload = json.dumps({"points": request.json.get("points"),"img_name": request.json.get("img_name")})r = request.post("http://192.168.0.106:8888/detect_points")print(r.text)return{"msg": "ok"}
from flask import Flask, request
import jsonapp = Flask(__name__)@app.route("/detect_points", methods=["POST"])
def hello_world():print("来自5000")data = request.get_data()print(data)data = json.loads(data.decode("utf-8"))print(data["img_name"])print(data["points"])return "Hello Marron1"if __name__ == "__main__":app.run(host="192.168.0.106", port=8888)