小程式POST請求 後臺SSM框架 挖坑
阿新 • • 發佈:2019-01-08
調了好久終於搞定了 總結一下
後臺程式碼 為兩種 一種封裝bean 一種直接傳形參 後臺框架為SSM框架
/* * 提交意見 * */ @RequestMapping(value = "giveAdvice", method = RequestMethod.POST) @ResponseBody public Integer giveAdvice(@RequestBody Advice advice) { System.out.println(advice); System.out.println("=========================" + advice.getUserId() + "================" + advice.getAdviceMsg()); return adviceService.giveAdvice(advice); } // 提交意見2 @RequestMapping(value = "giveAdvice2", method = RequestMethod.POST) @ResponseBody public Integer giveAdvice2(String UserId, String AdviceMsg) { // System.out.println(advice); System.out.println("=========================" + UserId + "================" + AdviceMsg); return adviceService.giveAdvice2(UserId, AdviceMsg); }
一、當小程式請求header為 'content-type': 'application/json' 時
wx.request({ url: appUrl+'/giveAdvice', data: { "UserId":app.globalData.userId, "AdviceMsg":advice }, header: { 'content-type': 'application/json' }, method: "POST", success(res){ console.log(res); } }) }
結果第一種giveAdvice API LOG資訊為
"=========================null================null”
新增失敗
giveAdvice 2 API LOG資訊為 "=========================null================null”
新增失敗
二、當小程式請求header為 'content-type': 'application/x-www-form-urlencoded'時
第一種 giveAdvice API log資訊為
DEBUG [http-nio-8080-exec-4] - Returning cached instance of singleton bean 'mobileController' DEBUG [http-nio-8080-exec-4] - Resolving exception from handler [public java.lang.Integer com.smartcharger.controller.MobileC ontroller.giveAdvice(com.smartcharger.pojo.Advice)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported DEBUG [http-nio-8080-exec-4] - Resolving exception from handler [public java.lang.Integer com.smartcharger.controller.MobileC ontroller.giveAdvice(com.smartcharger.pojo.Advice)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported DEBUG [http-nio-8080-exec-4] - Resolving exception from handler [public java.lang.Integer com.smartcharger.controller.MobileC ontroller.giveAdvice(com.smartcharger.pojo.Advice)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported DEBUG [http-nio-8080-exec-4] - Null ModelAndView returned to DispatcherServlet with name 'SmartCharger': assuming HandlerAdap ter completed request handling DEBUG [http-nio-8080-exec-4] - Successfully completed request
新增失敗
第二種 giveAdvice2 API log資訊為
新增成功!!!!!!!
終於成功了!!!!
結論 小程式的POST請求設定
header: {
'content-type': 'application/x-www-form-urlencoded'
}
後臺API介面 格式為
@RequestMapping(value = "giveAdvice2", method = RequestMethod.POST)
@ResponseBody
public Integer giveAdvice2(String UserId, String AdviceMsg) {
// System.out.println(advice);
System.out.println("=========================" + UserId + "================" + AdviceMsg);
return adviceService.giveAdvice2(UserId, AdviceMsg);
}
即可接收POST請求資訊!!!
除錯了2個小時終於有結果了 挖坑!!!