如何发布到HTTPs的?

假设我有一个形式,现在正在做一个帖子:

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

你会发现没有在没有网站 action ,浏览器遵循那里得到的页面从。

发帖时浏览器按照当前域规则

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 to ensure that the browser will always go to the *secure* page: http://stackoverflow.com/questions/ask https://stackoverflow.com/questions/ask/submit

通常你会尝试这样的:

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

除需要知道托管网 ​​站的域名和虚拟路径名称(如stackoverflow.com )。 如果网站发生了变化:

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

然后表单action将必须也更新:

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

我怎么能指示浏览器访问https 页面的版本?



假设语法:

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

--------------解决方案-------------

你可以用JavaScript改变形式的行动:

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

但也有一些安全方面的考虑,我建议你到表单的URL重定向到HTTPS。 虽然你可以从JavaScript这样做,你永远不应该相信的JavaScript当它到达安全性,因此从服务器上做(这也是比较快,页面没有加载,只有HTTP标头)

分类:HTML 时间:2015-03-15 人气:4
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.557 (s). 10 q(s)