JavaScript location.reload() is losing post data

2019-02-17 17:17发布

问题:

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

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

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

回答1:

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

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



回答2:

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



回答3:

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.

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

PLEASE STAR THIS BUG SO THAT IT GETS FIXED FASTER.



回答4:

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

$_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.

$_POST = £_SESSION['post'];

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



回答5:

location.reload does 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)



回答6:

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.



回答7:

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>