weixin_33725270 2019-04-21 16:42 采纳率: 0%
浏览 64

使用AJAX请求分派器

I'm sending a POST form using AJAX, and i'm trying to send back this value to my JSP, but I do not know exactly how to do it .

This is my AJAX.

 function ajax() {

    var e = document.getElementById("combo2");
    var e2 = e.options[e.selectedIndex].value;


    $.post( "MiddleContentApi", { cidades: e2})
        .done(function( data ) {
            alert( "Data Loaded: " + data );
        });
}

document.getElementById("okCity").addEventListener("click",ajax);

My Servlet :

   public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    Integer cidadeId = Integer.parseInt(request.getParameter("cidades"));
      System.out.print(cidadeId);
    response.setContentType("text/html;charset=UTF-8");
    response.getWriter().write(String.valueOf(cidadeId));
}

Ajax is working totally ok, the value is returning to my "data" and the alert(data) is working either, but what I really need is to access this value returned using scriptles <% %> in jsp, but if I try to do something like :

      public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

      Integer cidadeId = Integer.parseInt(request.getParameter("cidades"));
          request.setAttribute("numero",cidadeId);
          RequestDispatcher rd = request.getRequestDispatcher("estados.jsp");
          rd.forward(request,response);

            }

This's just not working, and I can't acess the data in jsp by using :

   Object jbo = request.getAttribute("numero");

Does someone now how can I do this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 chaquopy python 安卓
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 有没有帮写代码做实验仿真的
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥30 vmware exsi重置后登不上
    • ¥15 易盾点选的cb参数怎么解啊
    • ¥15 MATLAB运行显示错误,如何解决?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题