简体   繁体   中英

Retrieving POST data after redirect

I have read that the way to send data that needs to be accessible in the page I'm redirecting to can be solved using a form in JavaScript like follows:

$.post("{{=URL('leaveQuery')}}", {}, function(response) {
    var nextURL = JSON.parse(response).goto;
    var form = $('<form action="' + nextURL + '" method="post">' + '<input type="hidden" name="parameter1" value="sample" />' + '</form>');
    $('body').append(form);
    $(form).submit();
} 

However, now I am wondering how to retrieve this data now that I'm in the HTML page that has been redirected to?

If there is a better method to sending the POST data in the first place for a redirect, please let me know as well! I am a super newbie at this! Thanks in advance!

Unless the server in some way echoes the submitted form data on the page it serves after the redirect, there is no way for you to retrieve it using JavaScript alone. POST ed form data is sent in the HTTP request body; after the HTML form is submitted, the server-side code is the only part of your application that sees the submitted data, and it is its responsibility to process it appropriately. This could include feeding it back to the client. Now, if you were using the GET method, this would be a different story, since in this case data is transmitted through the URL, which remains visible to the client. Alternative workarounds include using cookies (not a great solution) or HTML5 local storage.

Think if it a high level: your client-side application submits data to your server in order to change its state in some way. The client then reloads the page from the server so that the client can render the page in such a way that it reflects the changed state on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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