jQuery - Redirect with post data

前端 未结 12 1567
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 20:14

How can I redirect with post data?

How to move to new page with $_POST?

How to do this? How is it done and whyfore shall it be done

相关标签:
12条回答
  • 2020-11-28 20:43

    I included the jquery.redirect.min.js plugin in the head section together with this json solution to submit with data

    <script type="text/javascript">     
        $(function () {
         $('form').on('submit', function(e) {
           $.ajax({
            type: 'post',
            url: 'your_POST_URL',
            data: $('form').serialize(),
            success: function () {
             // now redirect
             $().redirect('your_POST_URL', {
              'input1': $("value1").val(), 
              'input2': $("value2").val(),
          'input3': $("value3").val()
           });
          }
       });
       e.preventDefault();
     });
     });
     </script>
    

    Then immediately after the form I added

     $(function(){
         $( '#your_form_Id' ).submit();
        });
    
    0 讨论(0)
  • 2020-11-28 20:44

    "extended" the above solution with target. For some reason i needed the possibility to redirect the post to _blank or another frame:

    $.extend(
       {
           redirectPost: function(location, args, target = "_self")
           {
               var form = $('<form></form>');
               form.attr("method", "post");
               form.attr("action", location);
               form.attr("target", target);
               $.each( args, function( key, value ) {
                   var field = $('<input></input>');
                   field.attr("type", "hidden");
                   field.attr("name", key);
                   field.attr("value", value);
                   form.append(field);
               });
               $(form).appendTo('body').submit();
           }
       });
    
    0 讨论(0)
  • 2020-11-28 20:47

    Why dont just create a form with some hidden inputs and submit it using jQuery? Should work :)

    0 讨论(0)
  • 2020-11-28 20:51

    Here's a simple small function that can be applied anywhere as long as you're using jQuery.

    var redirect = 'http://www.website.com/page?id=23231';
    $.redirectPost(redirect, {x: 'example', y: 'abc'});
    
    // jquery extend function
    $.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>').appendTo('body').submit();
        }
    });
    

    Per the comments, I have expanded upon my answer:

    // jquery extend function
    $.extend(
    {
        redirectPost: function(location, args)
        {
            var form = $('<form></form>');
            form.attr("method", "post");
            form.attr("action", location);
    
            $.each( args, function( key, value ) {
                var field = $('<input></input>');
    
                field.attr("type", "hidden");
                field.attr("name", key);
                field.attr("value", value);
    
                form.append(field);
            });
            $(form).appendTo('body').submit();
        }
    });
    
    0 讨论(0)
  • 2020-11-28 20:51

    why not just use a button instead of submit. clicking the button will let you construct a proper url for your browser to redirect to.

    $("#button").click(function() {
       var url = 'site.com/process.php?';
       $('form input').each(function() {
           url += 'key=' + $(this).val() + "&";
       });
       // handle removal of last &.
    
       window.location.replace(url);
    });
    
    0 讨论(0)
  • 2020-11-28 20:57

    Before document/window ready add "extend" to jQuery :

    $.extend(
    {
        redirectPost: function(location, args)
        {
            var form = '';
            $.each( args, function( key, value ) {
    
                form += '<input type="hidden" name="'+value.name+'" value="'+value.value+'">';
                form += '<input type="hidden" name="'+key+'" value="'+value.value+'">';
            });
            $('<form action="'+location+'" method="POST">'+form+'</form>').submit();
        }
    });
    

    Use :

    $.redirectPost("someurl.com", $("#SomeForm").serializeArray());
    

    Note : this method cant post files .

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