首先我在vue.config.js 加上了devServer
module.exports = {
configureWebpack: config => {
},
devServer:{
proxy: {
"/api": {
target: "http://localhost:3000",
}
}
}
}
首先我用axios请求POST
addUser: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:178
xhrAdapter @ xhr.js?b50d:12
dispatchRequest @ dispatchRequest.js?5270:59
Promise.then (async)
request @ Axios.js?0a06:51
Axios.<computed> @ Axios.js?0a06:71
wrap @ bind.js?1d2b:9
REGUSER @ store.js?c0d6:14
wrappedMutationHandler @ vuex.esm.js?2f62:725
commitIterator @ 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"}}
所以我很困惑为什么会这样呢?
慕标5832272
噜噜哒
随时随地看视频慕课网APP
相关分类