How to redirect through 'POST' method using Javascript?

前端 未结 11 1074
说谎
说谎 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:43

    There is no way to do so.

    Even a a element Sstrikes the GET.

    What you can do is make a Ajax request to post and in on-success use window.open().

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

    I think you should construct and fill out a hidden method=POST action="YOUR_URL" form and submit it,

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

    Based on Eugene Naydenov's answer, I ended up using this which is able to fill form data also, hope to be useful for others:

    function redirectPost(url, data) {
        var form = document.createElement('form');
        document.body.appendChild(form);
        form.method = 'post';
        form.action = url;
        for (var name in data) {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = name;
            input.value = data[name];
            form.appendChild(input);
        }
        form.submit();
    }
    
    // redirectPost('http://www.example.com', { text: 'text\n\ntext' });
    
    0 讨论(0)
  • 2020-12-05 10:48

    Create a form, fill method and action attributes, submit the form.

    var redirect = function(url, method) {
        var form = document.createElement('form');
        form.method = method;
        form.action = url;
        form.submit();
    };
    
    redirect('http://www.example.com', 'post');
    

    jQuery version (but I'd prefer pure JavaScript in this particular case):

    var redirect = function(url, method) {
        $('<form>', {
            method: method,
            action: url
        }).submit();
    };
    
    redirect('http://www.example.com', 'post');
    
    0 讨论(0)
  • 2020-12-05 10:51

    Try this:

    var url = 'http://example.com/vote/' + Username;
    var form = $('<form action="' + url + '" method="post">' +
      '<input type="text" name="api_url" value="' + Return_URL + '" />' +
      '</form>');
    $('body').append(form);
    $(form).submit();
    
    0 讨论(0)
提交回复
热议问题