Java后台获取不到Fetch提交的数据



  • let uri = 'http://192.168.0.167/pharm/typeServlet';
    let formData = new FormData();
    formData.append("name", "admin");
    formData.append("password", "admin123");
    formData.append("page", "page");
    fetch(uri, {
    method: 'POST',
    mode: "cors",
    headers: {
    'Accept':'application/json',
    'Content-Type':'multipart/form-data;boundary=6ff46e0b6b5148d984f148b6542e5a5d'
    },
    body: "page=page",
    }).then((response) => response.json())
    .then((responseData) => {
    this.props.screenProps.getAccount(responseData);
    })
    .done();

    java后台代码:
    @Override
    final protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String page = req.getParameter("page");
    Object attribute = req.getAttribute("page");
    System.out.println(attribute);
    System.out.println("获取的json文件:" + page);
    }
    后台打印信息:
    null
    获取的json文件:null


  • administrators

    你直接page=page的形式,对应的content type是application/x-www-form-urlencoded 不是multipart/form-data

    如果你要提交formdata就不需要再自己指定content type


登录后回复