JavaScript:发送POST,重定向到响应

问题描述:

我有一张带有onclick的图像。当click事件触发时,我想发送一个HTTP POST并让window.location重定向到POST的响应。我该怎么做?

I have an image with an onclick. When the click event fires, I want to send an HTTP POST and have the window.location redirect to the response to the POST. How can I do that?

只需将按钮绑定到表单元素的submit方法,重定向就会自然发生。

Just bind the button to the submit method of a form element, and the redirect will happen naturally.

<script type='text/javascript'>
 function form_send(){
   f=document.getElementById('the_form');
   if(f){
     f.submit();
     }
   }
 </script>

<form method='post' action='your_post_url.html' id='the_form'>
  <input type='hidden' value='whatever'>
</form>

<span id='subm' onclick='form_send()'>Submit</span>