Domanda

i want to foward or redirect a page after a $.ajax call

$(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
        }
}); 
});
});

and on my server side i have this :

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;
     }   
}

I don't understand why when i click on my button it doesn't forward me to the resultat.jsp page ? what am i doing wrong ?

thanks for your awsers

È stato utile?

Soluzione

$.ajax calls are supposed to receive the response and do something, by sending it a 'success' function.

So I think the best to do in your case is return the full URL (with http://) and do this on your ajax request:

$.ajax({
    type: 'POST',
    url: '/resultat',
    data:{
            parametres : params,
            success : function(response){
                window.location.href = response;
        }
}); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top