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數(shù)據(jù)
JSONObject jsonRequest = GetJsonObject.getJsonObject(request);
// 獲取前端傳過來的當(dāng)前頁碼,和博主的賬號
String account = jsonRequest.getString("account");
int pageSize = jsonRequest.getInt("pageSize");
響應(yīng)前端:
?
?JSONObject json = new JSONObject();
? ? ? ? json.put("totalCount", totalCount);
? ? ? ? json.put("totalPage", totalPage);
// 將JSONObject對象轉(zhuǎn)換為JSON字符串
? ? ? ? String jsonString = json.toString();
// 將JSON字符串發(fā)送到前端
? ? ? ? response.getWriter().write(jsonString);
前端接收:文章來源:http://www.zghlxwxcb.cn/news/detail-621837.html
在axios的回調(diào)函數(shù)中.then(function (response) {
? ? ? ? ? ? console.log(response.data);)}
response.data就能拿到后端響應(yīng)過來的數(shù)據(jù)
?文章來源地址http://www.zghlxwxcb.cn/news/detail-621837.html
到了這里,關(guān)于JAVAWEB項目--POST完整交互(servlet,axios,JavaScript)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!