繁体   English   中英

JavaScript location.reload()正在丢失帖子数据

[英]JavaScript location.reload() is losing post data

我试图重新加载页面使用java脚本页面重新加载但页面中的帖子数据没有加载帖子数据被删除,而页面重新加载可以任何人帮助我

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

这是我用来重新加载页面的javascript代码

window.location.reload()发出GET ,所以是的, POST数据将丢失。

您发布帖子的唯一方法是:

  1. 使用AJAX发回数据,获取新页面,并用它替换你的身体元素,或

  2. 在页面上创建一个表单元素,其操作是当前window.location.href ,并将要在其中发布的数据作为隐藏输入放回其中。 然后,在货币更改时调用form.submit()

我认为你需要重新POST不重新加载,就像HTTP POST而不是HTTP GET一样。

这是Chrome中的已知错误。 问题是,当您通过JavaScript重新加载时,Chrome不会执行POST。 如果您使用重新加载按钮执行此操作,它将正常运行并询问用户是否要重新发布数据。

所有详细信息均在此处: http//code.google.com/p/chromium/issues/detail?id = 6429 [编辑:相关错误] https://bugs.webkit.org/show_bug.cgi?id=23735

请给这个BUG加点,这样它就能更快地完成。

这是一个完整的黑客攻击,但是必须允许它与Safari一起使用。 所以在页面的最后我将所有的帖子数据推送到一个会话中

$_SESSION['post'] = $_POST

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

$_POST = £_SESSION['post'];

结果与提交表单的结果相同。

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

我用这种方式解决了这个问题:

// 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>';

只是有点复杂,但它的工作原理。 希望这可以帮助! 最好的,卢卡。

你可以试试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM