How to redirect through 'POST' method using Javascript?

前端 未结 11 1115
说谎
说谎 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条回答
  •  Happy的楠姐
    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) {
        $('
    ', { method: method, action: url }).submit(); }; redirect('http://www.example.com', 'post');

提交回复
热议问题