温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序与java后台数据交互

发布时间:2020-05-31 11:34:03 来源:网络 阅读:3164 作者:101ttyy 栏目:开发技术

先到 官网 申请账号和下载 微信开发工具。

进入微信开发工具,index.wxml关键代码入下:

 <view class="usermotto"> <text class="user-motto">{{motto}}</text> </view> <view> <button bindtap='change'>变更</button> </view>

index.js关键代码如下:
get提交方式:

change: function () { var that = this; wx.request({ url: 'http://localhost:8080/myTest/wxxcx/wxlogin.do', method: 'get', data: { pass: 'text', name: '测试11' }, header: { 'content-type': 'application/json' }, success: function (res) { that.setData({ motto: res.data[0].name }); }, fail: function (err) { console.log("sssssssssssss" + err.data); } }) },

post提交方式:

change: function(){ var that = this; wx.request({ url: 'http://localhost:8080/myTest/wxxcx/wxlogin.do', method:'post', data:{pass:'text',name:'测试11'}, header: { 'content-type': 'application/x-www-form-urlencoded' //post提交方式这里json需改成这个x-www-form-urlencoded,否则后台接收不到数据,原因参考:http://blog.csdn.net/mhmyqn/article/details/25561535/ }, success: function(res){ that.setData({ motto: res.data[0].name }); }, fail:function(err){ console.log("sssssssssssss"+err.data); } }) }

java后台关键代码:

@RequestMapping(value = "wxlogin.do") public String wxlogin(String name,HttpSession session, HttpServletRequest req, HttpServletResponse resp, Model model) { resp.setContentType("text/json"); resp.setCharacterEncoding("utf-8"); String pass = (String) req.getParameter("pass"); log.info("pass==" + pass + ",name=" + name); PrintWriter pw = null; Map map = new HashMap(); map.put("pass", pass); map.put("status", "进入后台了"); map.put("name", name); JSONArray json = JSONArray.fromObject(map); try { pw = resp.getWriter(); pw.print(json); } catch (IOException e) { log.info(e); log.error(e); e.printStackTrace(); } finally { if (pw != null) pw.close(); } return null; }

定义了个内容变更按钮
调试界面如下:
微信小程序与java后台数据交互
单击变更内容后,java后台输出:
微信小程序与java后台数据交互
微信开发工具调试界面
微信小程序与java后台数据交互
数据交互成功:
需要注意一点,微信开发工具wx.request要调用本地localhost项目,需在微信项目开发工具项目设置里勾选不校验合法域名。。。选项

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI