JavaScript的location.reload()丢失了POST数据

9

我正在尝试使用JavaScript重新加载页面,页面已经重新加载,但是页面中的帖子数据没有加载。在页面重新加载时,帖子数据被删除了。有人能帮我解决这个问题吗?

function currencychange(xxx) {
    setTimeout('delay()', 2000);
}
function delay(){
    location.reload();
}

这是我用来重载页面的 JavaScript 代码,它会在 onchange 事件触发时自动执行。


通常在Joomla中使用Javascript重新加载页面是一个不好的想法。如果您试图在货币更改时更改页面上的其他值,为什么不使用getElementById来简单地替换onChange信息呢? - Cleanshooter
7个回答

9
window.location.reload()会发起GET请求,所以是的,POST数据将会丢失。
你可以通过以下两种方式发起POST请求:
1. 使用 AJAX 技术发起请求,将页面替换为新页面。 2. 创建一个 Form 元素,设置其 action 为当前的 window.location.href,将要提交的数据放在其中作为隐藏输入。然后,在货币变化时调用form.submit()方法来提交表单。

1
在Safari上实现这个功能需要进行一些骚操作,但是这是必要的。因此,在页面末尾,我将所有的帖子数据都推送到了一个会话中。
$_SESSION['post'] = $_POST

然后我使用jQuery来更新会话变量。完成更改后,使用location.reload();重新加载页面,当页面加载时,我简单地将所有会话数据推回post中。

$_POST = £_SESSION['post'];

结果与提交表单相同。

1

1

我认为你需要重新POST而不是重新加载,即使用HTTP POST而不是HTTP GET。


0
我是这样解决问题的:
// In the page called by POST, assuming coming from a search form 
// (i.e. searchResult.php)
$working=array();

if ($_POST) {
  if(!isset($_POST["postArray"]))  // Not set, first call
      $working = $_POST;
  else
     $working = unserialize($_POST["postArray"]); // recalled by someone else

  foreach ($working as $key => $value) { // Getting data
                $kv[] = "$key=$value";

// do something with input data....
           }

 echo '<form action="newRequest.php" method="post">'; 
// Your data here ......
 echo '<input type="hidden" name="currPost" value="' . htmlentities(serialize($working)) . '">';
 echo '<input type="submit" value="Go!">';
 echo '</form>';
 } // end if($_POST) ....


// This is the "newRequest.php" form
$postDataReceiver=array();
foreach ($_POST as $key => $value) { // Getting data from searchResult.php
           $kv[] = "$key=$value";

           switch($key) {
                // Your other stuff here ......
               case "currPost": // Here we are!
                    $postDataReceiver = unserialize($value);
                    break;


                 } // end switch
} // end foreach
echo '<form action="searchResult.php" method="post">'; 
// Your data here ......
 echo '<input type="hidden" name="postArray" value="' . htmlentities(serialize($postDataReceiver)) . '">';
 echo '<input type="submit" value="Go Back to search result!">';
 echo '</form>';

有点复杂,但它能够工作。希望这可以帮助你! 祝好,Luca。


0
你可以尝试这个:
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="form" id="form">

</form>

<script>
    // reload page every x miliseconds
    setInterval(function(){
    // inser here the post variables. Examples:

    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = 'id';
    input.value = <?=$id?>;

    document.form.appendChild(input);

    var input2 = document.createElement('input');
    input2.type = 'hidden';
    input2.name = 'anotherid';
    input2.value = <?=$anotherid?>;

    document.form.appendChild(input2);

    // send form
    document.form.submit();
    }, 120000);
</script>

0

location.reload 不应该提交任何数据。如果您需要将数据发送回服务器,请考虑使用 method='post' 提交表单(如果有的话),或者使用 AJAX(例如 jQuery.post


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接