小程序:前端同時傳遞String變量、JSON對象+後臺@RequestBody接收

純粹記錄試錯過程。

起因是想前端同時傳遞String變量、JSON對象,但是之前都是單純傳遞參數或者對象。

1 前端request請求

wx.request({
          url: "http://127.0.0.1:8080/superadmin/judgecheckresult?scanCode=" + that.data.scanCode,  // 在 url 中傳遞 String 
          data: JSON.stringify(formData),  // 將 formData 轉化成JSON對象
          method: 'POST',
          header: {'Content-Type': 'application/json'},  // http 請求是 JSON 數據格式
          success: function (res) {
            console.log("res")
            console.log(res)
          }
        })

HTTP Content-type 對照表 - 在線工具 - 開源中國社區

2 後臺接收數據

@RequestMapping(value = "/judgecheckresult", method = RequestMethod.POST)
    private Map<String, Object> judgeCheckResult(String scanCode, @RequestBody StuCheck stuCheck) {  // @RequestBody 序列化前端的 JSON 對象
    ...
}

這裏再說一下,後臺處理數據也可以用@RequestParam String來接收數據,但是此時String不能爲空,於是就用@RequestBody了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章