index.wxml
<view class="container"> <text>{{txt}}</text> <input name="name" type="text" id='text' bindchange="xyz"/> <button id="btn" bindtap="abc" >提交</button> </view>
index.js
//index.js //获取应用实例 const app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), txt:"", }, //事件处理函数 bindViewTap: function() { wx.navigateTo({ url: '../logs/logs' }) }, abc:function(e){//该函数用于和后台交互 // var v = e var v = this.txt; var self=this; //关键代码,这要操作程序无法运行 wx.request({ url: 'https://域名/jous/hello.do', //仅为示例,并非真实的接口地址 data: { name:v, }, header: { 'content-type': 'application/json' // 默认值 }, success(res) { //console.log(res.data) this.txt=res.data//把交互的参数值赋值给全局变量 console.log("aaa" + this.txt)//控制台输出变量 self.setData({//动态设置显示的值 txt: this.txt }) } }) }, xyz: function (e) {//从输入框获取数据 var v = e.detail.value this.txt=v;//将获取到的值赋值给中间变量 }, })
JAVA代码
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String msg = "Hello"+name; JsonUtil.printJson(response,msg); }
JAVA 转Json代码
public static void printJson(HttpServletResponse response, Object obj) throws IOException { // 返回数据,返回数据类型是json response.setContentType("application/json"); // 返回数据编码UTF-8 response.setCharacterEncoding("UTF-8"); // 返回数据,通过gson将数据返回给Ajax 通过gson工具提高工作效率 response.getWriter().write(new Gson().toJson(obj)); }