【问题标题】:foward on button click doesn't work单击按钮时前进不起作用
【发布时间】:2013-09-21 03:39:28
【问题描述】:

我想在 $.ajax 调用后转发或重定向页面

$(document).ready(function(){
$('#submit').on('click', function(){

    var params = "";
    $('#tableResultat > tbody > tr').each(function(){
        $parent = $(this);
        params += $parent.find('td:eq(0)').attr('id') + ","  + $parent.find('td:eq(0)').text() + "," +  $parent.find('td:eq(1)').text() + ","  + $parent.find('td:eq(2)').text() +','+ $parent.find('td:eq(3)').text() +"|";
    });

$.ajax({
    type: 'POST',
    url: '/resultat',
    data:{
            parametres : params
        }
}); 
});
});

在我的服务器端我有这个:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException 
{
     String action = req.getServletPath();
     if(action.equals("/resultat"))
     {
         String params = req.getParameter("parametres");
        //save these value in DB 
         req.getRequestDispatcher("WEB-INF/vues/resultat.jsp").forward(req, resp);
         return;
     }   
}

我不明白为什么当我单击按钮时它没有将我转发到 resultat.jsp 页面?我做错了什么?

感谢您的支持

【问题讨论】:

标签: javascript ajax post redirect forward


【解决方案1】:

$.ajax 调用应该通过向其发送“成功”函数来接收响应并执行某些操作。

所以我认为在你的情况下最好的做法是返回完整的 URL(带有 http://)并在你的 ajax 请求上执行此操作:

$.ajax({
    type: 'POST',
    url: '/resultat',
    data:{
            parametres : params,
            success : function(response){
                window.location.href = response;
        }
}); 

【讨论】:

  • 我做了:resp.getOutputStream().println("/resultat");它有效,但我仍然不知道为什么前锋一开始就不起作用!谢谢你的遮阳篷,我想投票,但我不能,因为我是 stackOverflow 的新手。
猜你喜欢
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2017-03-09
  • 1970-01-01
相关资源
最近更新 更多