通过链接导航到具有发布请求的另一个页面 [英] navigate to another page with post request through link

查看:21
本文介绍了通过链接导航到具有发布请求的另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>    
<script type="text/javascript">
    function DoPost(){
      $.post("index.html", { name: "John", time: "2pm" } );
   }
    </script>
</head>

<body>
<a href="javascript:DoPost()">GO</a>

</body>
</html>

我创建了函数并尝试调用该函数,在该函数中我提到了 here 中提到的 url 和 data.但是,它对我不起作用.

I made function and trying to call that function, inside that function I mentioned url and data as mentioned here. But, It's not working for me.

注意:即使我在帖子标题中提到过,我也想澄清一下,我想通过简单的超链接使用 POST 方法导航到另一个页面.

NOTE : Even I mentioned in my post title, then also I want to clarify that, I want to navigate to another page using POST method through simple hyperlink.

推荐答案

使用您需要发送的所有数据创建一个 html 表单,并将您需要转发给用户的页面指定为操作.

Create an html form with all the data you need to send and specify as action the page you need to forward the user.

<form method="post" id="theForm" action="REDIRECT_PAGE.php">

然后在该表单中放入一些隐藏字段.

Then put some hidden fields in that form.

<input type="hidden" name="name" value="John">
<input type="hidden" name="time" value="2pm">
</form>

将其包装在您的 doRedirect 函数中,重定向将在正确提交您的 POST 数据的同时工作.

Wrap this inside of your doRedirect function and the redirect will work while correctly submitting your POST data.

document.getElementById('theForm').submit()

<小时>

作为旁注,如果您需要读取 POST 数据,您可能希望将用户重定向到 .php 页面而不是 .html 页面.这取决于您的服务器配置,但默认情况下,我认为您不能在 .html 文件中运行 PHP 代码.


As a side note, you may want to redirect the user to a .php page not a .html one if you need to read POST data. This depends on your server configuration but, by default, I don't think you can run PHP code inside of a .html file.

这篇关于通过链接导航到具有发布请求的另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆