首先我在vue.config.js 加上了devServermodule.exports = { configureWebpack: config => { }, devServer:{ proxy: { "/api": { target: "http://localhost:3000", } } } } 首先我用axios请求POSTaddUser:function () { axios.post('/api/reguser?name=sds&password=2&age=1&sex=man&company=ali') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); } 然后我用Servlet的req.getParameter的方法获取前端传来的POST字段!我可以正常接受!然后我对数据做了封装!addUser:function () { axios.post('/api/reguser',{ data }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); } 那么出现了一个问题就是axios会把data放在请求的body中,我后台获取不到这样的数据了并且前端会报错误xhr.js?b50d:178 POST http://localhost/api/reguser 500 (Internal Server Error)dispatchXhrRequest @ xhr.js?b50d:178xhrAdapter @ xhr.js?b50d:12dispatchRequest @ dispatchRequest.js?5270:59Promise.then (async)request @ Axios.js?0a06:51Axios.<computed> @ Axios.js?0a06:71wrap @ bind.js?1d2b:9REGUSER @ store.js?c0d6:14wrappedMutationHandler @ vuex.esm.js?2f62:725commitIterator @ vuex.esm.js?2f62:391(anonymous) @ vuex.esm.js?2f62:390于是我查了相关问题,顺利的解决了后端接受不到json对象的问题StringBuilder builder = new StringBuilder(); try { BufferedReader bufferedReader = req.getReader(); char[] buff = new char[1024]; int len; while((len = bufferedReader.read(buff)) != -1){ builder.append(buff,0,len); } }catch (IOException e){ System.out.println(e); }但是我前端每次请求都会报错POST http://localhost/api/reguser 500 (Internal Server Error)但是后端能正常的接受到数据{"data":{"name":"23","password":"23","age":"23","sex":"男","company":"23"}}所以我很困惑为什么会这样呢?
1 回答

噜噜哒
TA贡献1784条经验 获得超7个赞
getParameter
拿的是 queryString 或者 表单 的数据
如果需要拿json就要自己读 body、反序列化
500是后端报错,看后端的报错输出
添加回答
举报
0/150
提交
取消