小程序请求后端接口实例
小程序wxml页面button 按钮
<view> <button bindtap="change">点击</button> //change 为js函数名 </view> 小程序js页面函数 Page({ change: function(event){ wx.request({ url: 'http://localhost:80/services/login/username', //如果不设置method 则默认get请求地址
method: 'post', data: { //可携带参数
},
header: { 'content-type': 'application/json' // 默认值 }, success: function(res) { console.log(res.data) //res.data 为接口返回值 }, }) } }) springboot后端接口: @RestController @RequestMapping("/login") public class loginController { @RequestMapping(value = "/username",method = RequestMethod.POST) public String login(HttpServletRequest request){ System.out.println(request.getParameter("userName")); return "请求成功"; } }
//spring后端接口以及接收参数的方式
@Component @Scope("prototype") @Path("/medical") public class MedicalRecordAction { @POST @Path("/login") @Produces(value = MediaType.TEXT_PLAIN) public String login(@FormParam(value = "requestJson") String requestJson){ return medicalInsertService.login(requestJson); }
//小程序前端请求方式以及请求参数 wx.request({ url: app.globalData.webServiceUrl+'/medical/login', method: 'post', data: { requestJson:"{'accountid':\""+app.globalData.accountid+"\",}", }, header: {'content-type': 'application/x-www-form-urlencoded' }, success:function(res){ if(res.data.code == 0){ for (let index in res.data.responseData) { //获取userid并配置全局userid app.globalData.userid = res.data.responseData[index].userid } }else{ wx.showToast({ title: res.data.responseData, icon: "none", }) } } })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】