How to POST for HTTP?

Suppose I have a form that is making a message right now:

<form id="post-form" class="post-form" action="/questions/ask/submit" method="post"> 

You will notice that there is no site in action , the browser follows where it got the page from.

The browser follows the current domain rules when publishing

 If the current page is... then the browser will POST to ======================================= ============= http://stackoverflow.com/questions/ask http://stackoverflow.com/questions/ask/submit https://stackoverflow.com/questions/ask https://stackoverflow.com/questions/ask/submit 

But I want the browser to always go to a secure page:

 http://stackoverflow.com/questions/ask https://stackoverflow.com/questions/ask/submit 

Usually you try something like:

 <form id="post-form" class="post-form" action="https://stackoverflow.com/questions/ask/submit" method="post"> 

Except that it requires knowledge of the domain and the name of the virtual path for the hosting site (for example, stackoverflow.com ). If the site has been changed:

  • stackoverflow.net
  • stackoverflow.com/mobile
  • de.stackoverflow.com
  • stackoverflow.co.uk/fr
  • beta.stackoverflow.com

then it would also be necessary to update the action form:

 <form id="post-form" class="post-form" action="https://stackoverflow.net/questions/ask/submit" method="post"> <form id="post-form" class="post-form" action="https://stackoverflow.com/mobile/questions/ask/submit" method="post"> <form id="post-form" class="post-form" action="https://de.stackoverflow.com/questions/ask/submit" method="post"> <form id="post-form" class="post-form" action="https://stackoverflow.co.uk/fr/questions/ask/submit" method="post"> <form id="post-form" class="post-form" action="https://beta.stackoverflow.com/questions/ask/submit" method="post"> 

How can I tell the browser to switch to the https version of the page ?


Hypothetical syntax:

 <form id="post-form" class="post-form" action="https://./questions/ask/submit" method="post"> 
+8
html google-chrome internet-explorer-9
source share
1 answer

you can change the action of the form using javascript:

 var form = document.getElementById("post-form"); form.action = location.href.replace(/^http:/, 'https:'); 

But there are some security considerations, and I suggest that you redirect the form URL to https. And although you can do it from javascript, you should never trust javascript when it gets into the security system, so do it from the server (it is also faster, the page does not need to be loaded, only the http header)

+3
source

All Articles