Send post-variable with javascript?

前端 未结 5 1005
逝去的感伤
逝去的感伤 2021-01-05 19:55

Is it possible to send post-variables with javascript? I want id to be sent with post, not get.

window.location.href=\"hanteraTaBortAnvandare.ph         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 20:10

    You can use a form and then document.getElementById('id_of_the_form').submit();

    The form doesn't need to be wrote as plain HTML: you can create it dinamically:

         function postIt()   {
            form = document.createElement('form');
            form.setAttribute('method', 'POST');
            form.setAttribute('action', 'someURL');
            myvar = document.createElement('input');
            myvar.setAttribute('name', 'somename');
            myvar.setAttribute('type', 'hidden');
            myvar.setAttribute('value', 'somevalue');
            form.appendChild(myvar);
            document.body.appendChild(form);
            form.submit();   
    }
    

提交回复
热议问题