问题
I have two values one for name and another for password that I would like to pass to another page if the user entered the right combination. If the user did I know that I can redirect by including the values in the query string like:
$cgi->redirect('http:someotherpage.com?username=$username&password=$password');
but that is using a GET request, is there a way to do the same thing using a POST or is there another way to pass on the values when redirecting in Perl? I know that JSP has a method called RequestDispatcher is there something like that in Perl if the POST is not available?
Edit: I found this link CGI Application Dispatch earlier but it's a little technical so I don't know if this is the right tool for the job.
Edit: I'm using CGI.pm
回答1:
When doing a HTTP redirect you can only give the URL of the new target. This means, there is no way to specify any data for the POST request, because they are outside the URL.
From my understanding the RequestDispatcher from JSP does not send a HTTP redirect response, but forwards the current request internally inside the JSP application. This would be comparable to let your CGI script just dispatch the current request to another script on the same server, without doing a redirect through the browser. Details how to do this would depend on the framework you use (e.g. Dancer, Mojolicious, Catalyst ... or plain CGI.pm)
回答2:
I had a need to redirect a page and include POST data. Searching for a solution only uncovered posts saying that it was impossible to do. So... challenge accepted.
First the why: I have a perl cgi that is called with POST data, and puts up a form (text input, checkboxes, etc.) based on the passed data. The user makes their changes/selections and submits the form. The target is a cgi that processes the POST data and performs the necessary updates to a database. I then wanted to immediately redisplay the first form, so the results of the changes could be seen. While there is minimal POST data necessary to display the form, I was loath to send it as a GET.
My solution:
After processing the POST data, the cgi generates another HTML page, which includes:
- A form with the POST data to be passed and desired target
- A javascript function that submits the form
- An 'onload' event in the BODY tag that executes the javascript
The loading of the above page is almost unnoticeable (if I didn't know it was happening, I probably wouldn't be aware of it.) And it works like a charm.
来源:https://stackoverflow.com/questions/25353474/how-do-i-send-values-with-post-when-redirecting-in-perl