1. 用代码创建服务
const app = express();
app.use(bodyParser.json());
//处理POST请求
app.post("/", (req, res) => {
console.log(req.body);
res.json(req.body);
});
// 监听3000端口
app.listen(3000, () => {
console.log("server running at http://127.0.0.1:3000");
});
2. 利用wx.request()向本地HTTP服务器发送POST请求:
submit: function (e) {
wx.request({
method: "post",
url: "http://127.0.0.1:3000",
data: e.detail.value,
success: function (res) {
console.log(res);
},
});
},
3. 请求接口获取后台数据
onLoad: function (options) {
wx.request({
url: "http://127.0.0.1:3000",
success: (res) => {
this.setData(res.data);
},
});
},
app.get('/',(req,res) => {
res.json(data)
})
网友评论