问题:微信小程序使用post请求,java后台没放应,报400,找不到路径,出现这个问题也是因为我单独将url封装到utils的工具包下的,在指定页面的js文件下是没问题的,以下是自己临时的解决方案。
wx.request({
url: xyUrl + url,
data: data,
async: false,
dataType: "json",
method:'GET',
header: {
'content-type': 'application/json' // 默认值,这个值如果使用post请求的话,java后台完全没反应
},
排查发现是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");
//执行注册逻辑
wxService.reg(url_openid,appid, secret, jsCode, grant_type);
HttpResult httpResult = new HttpResult(200,"新增成功");
JSONObject jsonObj = (JSONObject) JSON.toJSON(httpResult);
return jsonObj;
}












网友评论