JAVAWEB项目--POST完整交互(servlet,axios,JavaScript)
post交互
js:
axios.post("/mycsdn/blog/pageSer", {currentPage:currentPage,}).then(function (response) {window.location.href = url;}).catch(function (error) {console.error("分页未遂", error);});
后端servlet:
public void getTotalSer(HttpServletRequest request,HttpServletResponse response) throws IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html; charset=utf-8"); //json数据JSONObject jsonRequest = GetJsonObject.getJsonObject(request);// 获取前端传过来的当前页码,和博主的账号String account = jsonRequest.getString("account");int pageSize = jsonRequest.getInt("pageSize");
响应前端:
JSONObject json = new JSONObject();json.put("totalCount", totalCount);json.put("totalPage", totalPage);// 将JSONObject对象转换为JSON字符串String jsonString = json.toString();
// 将JSON字符串发送到前端response.getWriter().write(jsonString);
前端接收:
在axios的回调函数中.then(function (response) {console.log(response.data);)}
response.data就能拿到后端响应过来的数据