問題:微信小程序使用post請求,java后臺沒放應,報400,找不到路徑,出現(xiàn)這個問題也是因為我單獨將url封裝到utils的工具包下的,在指定頁面的js文件下是沒問題的,以下是自己臨時的解決方案。
wx.request({
url: xyUrl + url,
data: data,
async: false,
dataType: "json",
method:'GET',
header: {
'content-type': 'application/json' // 默認值,這個值如果使用post請求的話,java后臺完全沒反應
},
排查發(fā)現(xiàn)是header的問題,奇怪的是這是官方推薦的

image.png
解決方案:
將header的值改為以下這個又可以了,微信官方有點騷啊
wx.request({
url: xyUrl + url,
data: data,
async: false,
dataType: "json",
method:'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
后臺代碼
/***
* 微信自動通過后臺進行注冊邏輯
* @param request
* @return
* @throws Exception
*/
@RequestMapping(value = "/reg",method=RequestMethod.POST)
@ResponseBody
public JSONObject reg(HttpServletRequest request) throws Exception {
String jsCode = request.getParameter("code");
//執(zhí)行注冊邏輯
wxService.reg(url_openid,appid, secret, jsCode, grant_type);
HttpResult httpResult = new HttpResult(200,"新增成功");
JSONObject jsonObj = (JSONObject) JSON.toJSON(httpResult);
return jsonObj;
}