jQuery post 没有表单但有页面刷新

jQuery post without a form but with page refresh

是否可以仅使用 jQuery 将 POST 数据提交到 PHP 脚本,而无需页面上的表单并刷新页面?

重定向

$(document).ready(function(){
    $('.button').click(function(){
        window.location.replace('index.php?some_value=1');
    });
});

在此示例中发送了 GET 数据,但如何使用此重定向发送 POST 数据?

POST

$(document).ready(function(){
    $('.button').click(function(){
        $.post(
            'index.php',
            {
                some_value: '1'
            }, function(result) {
                alert(result);
            }
        );
    });
});

这个例子不会刷新页面。

您可以隐藏表单,以便将数据包含在表单中,然后 "submit it" 您自己。

如果您包括

style="display:none"

在你的表格中这样

<form style="display:none" action="POST">
    <input field your input field>
    <input maybe another input field>
</form>

那么您仍然可以通过 post 提交内容,但不会显示