wx.request使用
代碼如下:
wx.request({ url: 'https://192.168.191.1:8443/vocation/bbb', data: {name:"111",password:"123",id:"123"}, method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT // header: {}, // 設置請求的 header // dataType:JSON,//該語句會將服務器端的數據自動轉為string類型 success: function(res){ // success console.log("返回數據為:"+res.data.employees[1].firstName); console.log('submit success'); }, fail: function() { // fail console.log('submit fail'); }, complete: function() { // complete console.log('submit comlete'); } })
服務器端的代碼:
JSONObject jsonObject = JSONObject.fromObject(getRequestPayload(request)); String name=jsonObject.get("name").toString(); String password=jsonObject.get("password").toString(); String s = ""; response.setContentType("application/json;charset=utf-8"); response.setHeader("Access-Control-Allow-Origin", "*"); System.out.println(name + " " + password); s = "{\"employees\": [{ \"firstName\":\"John\" , \"lastName\":\"Doe\" },{ \"firstName\":\"Anna\" , \"lastName\":\"Smith\" },{ \"firstName\":\"Peter\" , \"lastName\":\"Jones\" }]}"; response.getWriter().write(s);
數據傳送到微信端是[object object]的形式,因此直接可以通過res.data.employees[1].firstName獲得對象的數據