使用axios发送多个参数的post请求(Servlet)

正确示例

使用字符输入流的方式来读取请求参数

  • html
    	<!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        window.onload = function () {
            axios({
                method: "post",
                url: "http://localhost:8080/ajax-test/test",
                data: {
                    people: {
                        name: '张三',
                        age: 18
                    }
                }
            }).then(function (resp) {
                console.log(resp)
            })
        }
    </script>
    </body>
    
    </html>
    
  • servlet
    @WebServlet("/test")
    public class AxiosTest extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf8");
            System.out.println(req.getReader().readLine());
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }
    
  • 控制台输出
    {"people":{"name":"张三","age":18}}
    

错误示例

  • 如果servlet中是通过getParameter()来获取请求参数,是获取不到的

    @WebServlet("/test")
    public class AxiosTest extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf8");
            Object people = req.getParameter("people");
            System.out.println(people);  // 输出结果是 null
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }
    
  • 原因:
    getParameter()获取的是请求头中的参数,而post请求的数据被封装在了请求体
    使用axios时,会将数据变成json字符串,并通过字节流传递(服务器通过字节流接收),并且只有一行

  • 如果希望使用getParameter()来获取参数,并且请求的方式仍然是post,可以将参数放到url
    例如:

    • axios:
       axios({
           method: "post",
           url: "http://localhost:8080/ajax-test/test?msg=hello",
           data: {
               people: {
                   name: '张三',
                   age: 18
               }
           }
       }).then(function (resp) {
           console.log(resp)
       })
      
    • servlet
      @WebServlet("/test")
      public class AxiosTest extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              req.setCharacterEncoding("utf8");
              System.out.println(req.getParameter("msg"));
          }
      
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              this.doGet(req, resp);
          }
      }
      
    • 控制台输出
      hello
      
      在这里插入图片描述
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值