如何使用post方法创建重定向链接

How can create a link to redirect with post method

本文关键字:重定向 链接 创建 方法 何使用 post      更新时间:2023-09-26

我想创建一个链接,用POST数据方法重定向到页面。

有什么简单的函数可以用Jquery实现这一点吗?

有些东西像下面的代码(没有重定向到页面)

<script language="javascript"> 
function DoPost(id){
 $.post("http://www.jooyeshgar.dev/desktop/module.php?op=users&f=email", { fn: "read", time: id } );  //Your values here..
}
</script>

<a href='javascript:DoPost({$row_email['id']})'>

感谢您的帮助

似乎您基本上想要使用a而不是按钮来提交表单(在填充了一些值之后)。

最简单(也是纯javascript)的解决方案是在页面上有一个form(隐藏,因为它不包含可见元素)(或者动态创建并附加一个):

<form id="myform" method="POST" 
  action="http://www.jooyeshgar.dev/desktop/module.php?op=users&f=email">
  <input type="hidden" name="fn" value="read" />
  <input type="hidden" name="time" value="" />
</form>
<a href="#" onclick="return !!DoPost(/*your string*/);">Submit</a>

在javascript中:

function DoPost(id){
  var frm=document.getElementById('myform');
  frm.time.value=id; 
  frm.submit();
}

然后让浏览器完成它的工作:)因为这似乎是你想要的行为
这就是基础,您可以基于上述概念将其转换为jQuery。

希望这能有所帮助!

如果您在程序中使用JSP,请按照

有一个在JSP中从用户那里获取数据的表单。并将表单方法设置为"post"

创建一个名为FormServlet的servlet,对于jsp中的表单操作,将其命名为action="FormServlet"

然后从servlet的使用,

response.sendRedirect("nameofyourpage");

希望这能对你有所帮助。

在$.post的成功回调函数中,您可以使用window.location.replacement()方法替换url:

$.post("http://www.jooyeshgar.dev/desktop/module.php?op=users&f=email", { fn: "read", time: id }, function(){ window.location.replace('your url.'); } );  

供参考:http://api.jquery.com/jquery.post/