溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序與java后臺數據交互

發布時間:2020-05-31 11:34:03 來源:網絡 閱讀:3165 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女