gpt4 book ai didi

javascript - 通过链接使用发布请求导航到另一个页面

转载 作者:技术小花猫 更新时间:2023-10-29 12:14:11 24 4
gpt4 key购买 nike

<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>

我创建了函数并尝试调用该函数,在该函数中我提到了 url 和数据,如前所述 here .但是,它对我不起作用。

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

最佳答案

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

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

然后在该表单中放置一些隐藏字段。

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

将其包装在您的 doRedirect 函数中,重定向将在正确提交您的 POST 数据时起作用。

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

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

关于javascript - 通过链接使用发布请求导航到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17378619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com