JavaScript location.reload() 正在丢失发布数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13476006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:54:01  来源:igfitidea点击:

JavaScript location.reload() is losing post data

phpjavascriptjoomla

提问by deepak

I am trying to reload the page using java script the page reloads but the post data in the page is not loading the post data are deleted while the page reloads can any one help me with it

我正在尝试使用 java 脚本重新加载页面页面重新加载但页面中的帖子数据未加载帖子数据在页面重新加载时被删除,任何人都可以帮助我

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

this is the javascript code which I am using to reload the page onchange

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

采纳答案by Steven Moseley

window.location.reload()issues a GET, so yes, the POSTdata will be lost.

window.location.reload()问题 a GET,所以是的,POST数据将丢失。

The only ways you can issue a post are either:

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

  1. Use AJAX to post the data back, get the new page, and replace your body element with it, or

  2. Create a form element on the page with the action being the current window.location.href, and place the data you want to post back inside it as hidden inputs. Then, on currency change call form.submit()

  1. 使用 AJAX 回传数据,获取新页面,并用它替换你的 body 元素,或者

  2. 在页面上创建一个表单元素,动作为 current window.location.href,并将您想要回传的数据作为隐藏输入放置在其中。然后,在货币变化调用form.submit()

回答by John U

I think you need to re-POST not re-load, as in HTTP POST rather than HTTP GET.

我认为您需要重新发布而不是重新加载,就像在 HTTP POST 而不是 HTTP GET 中一样。

回答by Emmanuel

This is a known bug in Chrome. The issue is that Chrome doesn't do a POST when you reload via JavaScript. If you did that using the reload button, it would behave properly and ask the user if he wants to repost the data.

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

All the details are here: http://code.google.com/p/chromium/issues/detail?id=6429[Edit: related bug] https://bugs.webkit.org/show_bug.cgi?id=23735

所有细节都在这里:http: //code.google.com/p/chromium/issues/detail?id=6429[编辑:相关错误] https://bugs.webkit.org/show_bug.cgi?id=23735

PLEASE STAR THIS BUG SO THAT IT GETS FIXED FASTER.

请为这个错误加星号,以便它更快地得到修复。

回答by Patrick McCarthy

This was a complete hack, but necessary and allowed it to work with Safari. So at the end of the page I pushed all the post data into a session

这是一个完整的黑客,但必要的,并允许它与 Safari 一起使用。所以在页面的末尾我把所有的帖子数据推到了一个会话中

$_SESSION['post'] = $_POST

I then used jQuery to update the session variables. Then used location.reload(); to reload the page once I was completed making changes and the as the page loads I simple pushed all session data back into post.

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

$_POST = £_SESSION['post'];

The result is the same as if a form was submitted.

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

回答by R?mulo Z. C. Cunha

You can try this:

你可以试试这个:

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

回答by Pentolone

I solved the problem in this way:

我是这样解决问题的:

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

Just a little bit complicated, but it works. Hope this can help! Best, Luca.

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

回答by J0HN

location.reloaddoes not supposed to post any data. If you need to send your data back to server consider submitting the form with method='post'(if you have one), or use AJAX (e.g. jQuery.post)

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