How to redirect through 'POST' method using Javascript?

前端 未结 11 1073
说谎
说谎 2020-12-05 10:05

I\'ve queried and doesn\'t work out what I\'ve found. Is there any way to redirect to give url with POST method using Javascript or jquery?

相关标签:
11条回答
  • 2020-12-05 10:24

    Redirect with POST vars (on jQuery)

    var redirect = 'http://www.website.com/page?id=23231';
    $.redirectPost(redirect, {x: 'example', y: 'abc'});
    
    $.extend(
    {
        redirectPost: function(location, args)
        {
            var form = '';
            $.each( args, function( key, value ) {
                form += '<input type="hidden" name="'+key+'" value="'+value+'">';
            });
            $('<form action="'+location+'" method="POST">'+form+'</form>').submit();
        }
    });
    
    0 讨论(0)
  • 2020-12-05 10:29

    Here the idea is to send data to server while you redirect user to another webpage without using the GET method and maintaining a cosmetic appearance to your URL. So we are going to use the same procedure of sending a Form with POST method.

    HTML/Javascript code for creating the form and submitting it to server:

    <html>
    <body>
    <script>
    var form = document.createElement("form");
    form.method = 'post';
    form.action = 'receive.php';
    var input = document.createElement('input');
    input.type = "text";
    input.name = "data";
    input.value = "this is the data I'm sending to server through POST";
    form.appendChild(input);
    form.submit();
    </script>
    <body>
    <html>
    

    Receiving this data in PHP:

    <pre>
    <?php
    var_dump($_POST);
    ?>
    </pre>
    

    In this way, users will be redirected to receive.php with some data being informed in a POST method.

    0 讨论(0)
  • 2020-12-05 10:29

    You can use serialize your form and all the data in post. Below is an example.

    $("#submit_btn").click(function(){
            $('.error_status').html();
                if($("form#frm_message_board").valid())
                {
                    $.ajax({
                          type: "POST",
                          url: "<?php echo site_url('message_board/add');?>",
                          data: $('#frm_message_board').serialize(),
                          success: function(msg) {
                              var msg = $.parseJSON(msg);
                              if(msg.success=='yes')
                              {
                                                                                return true;
                             }
                             else
                             {
                                alert('Server error');
                                return false;
                            }
                           }
                    });
                }
                return false;
            });
    
    0 讨论(0)
  • 2020-12-05 10:32

    huh... use AJAX Jquery:

    > $.ajax({
    >                       type: "POST",
    >                       url: "www.example.com/the_page_I_post_to.php",
    >                       data: $('yourform').serialize(),//sent data
    >                       success: function(msg) {
    >                           alert("posted !");
    >                        }
    
    0 讨论(0)
  • 2020-12-05 10:33

    Using jQuery post the data and redirect to your desired location like so:

    $.ajax({
      type: 'POST',
      url: 'receivedata.asp',
      data: 'formValue=someValue',
      success: function(data){
          window.location = data;
      }
    });
    

    You could remove the data: 'formValue=someValue', if there is no data to pass to the page you want to post to.

    0 讨论(0)
  • 2020-12-05 10:40

    Actually there is a trick. You create a hidden form and trigger the submit button, with jQuery for example:

    <form method="POST" action="newurl.html" id="myform">
    </form>
    

    and do a

    $('#myform').submit();
    

    I suggest you to use the no-jQuery version posted by Eugene Naydenov

    0 讨论(0)
提交回复
热议问题