Redirect page with post using ajax

Asked

Viewed 97 times

1

I know the question is very strange, but it is a need that we have, I need to move to another page some variables that I rescued, but it is not only pass but redirect the page as well. I can’t use a window.Location, it needs to be by post.

What I have at the moment, the return is showing me the variables I need to pass, but as I said I need to redirect.

function preparaPagina() {

    $('button.inline-button').click(function() {

        var self = $(this);
        var IdCota = self.siblings('.product-info').data('idcota');
        var IdProduto = self.siblings('.product-info').data('produto');
        var IdUsuario = self.siblings('.product-info').data('usuario');
        var NomeUser = self.siblings('.product-info').data('nome');

        dados = {"IdCota": IdCota, "IdProduto": IdProduto, "IdUsuario": IdUsuario, "NomeUser": NomeUser  };

    $.ajax({
        type: 'POST',
        url: 'iCadAutorizacaoTrans.php',
        data : dados,
        success:function(data){
            console.log(data);
        }
    });
    })
}

preparaPagina();
  • window.location inside the sucess callback would not work?

  • If not I didn’t understand this: I can’t use a window.Location, it needs to be by post.

  • That even Marconi, I need to pass these variables and redirect to the page I pop sending.

1 answer

2


In function success from Ajax you can insert a form on the page placing the values in each input with their respective name and submit it via method="post", redirecting the page to the destination page declared in the variable url_ (see explanatory comments in the code):

function preparaPagina(){

   $('button.inline-button').click(function(){

      var self = $(this);
      var IdCota = self.siblings('.product-info').data('idcota');
      var IdProduto = self.siblings('.product-info').data('produto');
      var IdUsuario = self.siblings('.product-info').data('usuario');
      var NomeUser = self.siblings('.product-info').data('nome');

      var dados = {"IdCota": IdCota, "IdProduto": IdProduto, "IdUsuario": IdUsuario, "NomeUser": NomeUser  };

      // url do post
      var destino = 'iCadAutorizacaoTrans.php';

      $.ajax({
         type: 'POST',
         url: destino,
         data : dados,
         success:function(data){
            console.log(data);

            // monta o formulário
            // note que o atributo "hidden" faz com que ele fique oculto
            var formulario = '<form id="form" hidden method="post" action="'+destino+'">'
            +'<input name="IdCota" value="'+IdCota+'">'
            +'<input name="IdProduto" value="'+IdProduto+'">'
            +'<input name="IdUsuario" value="'+IdUsuario+'">'
            +'<input name="NomeUser" value="'+NomeUser+'">'
            +'</form>';

            // insere o formulário no body
            $("body").append(formulario);
            // faz o submit e remove o formulário
            $("#form").submit().remove();

         }
      });
   })
}

preparaPagina();
  • 1

    Excellent, thanks a lot @DVD, helped me too, thanks a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.