首先我在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字段!我可以正常接受!然后我對數(shù)據(jù)做了封裝!addUser:function () { axios.post('/api/reguser',{ data }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); } 那么出現(xiàn)了一個問題就是axios會把data放在請求的body中,我后臺獲取不到這樣的數(shù)據(jù)了并且前端會報錯誤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)但是后端能正常的接受到數(shù)據(jù){"data":{"name":"23","password":"23","age":"23","sex":"男","company":"23"}}所以我很困惑為什么會這樣呢?
關于Vue.axios 與 Servlet POST請求問題
慕標5832272
2019-05-19 14:05:29