如何让浏览器记住密码AJAX的形式? [英] How to make Chrome remember password for an AJAX form?
问题描述
我使用AJAX在我的登录页快速输入验证。如果一切正常,用户将被重定向。
I'm using AJAX for fast input validation on my login page. If everything is correct, the user is redirected.
这里的code:
$(form).submit(function () {
$.post($(this).attr('action'), $(this).serialize(), function (data) {
if (data.status == 'SUCCESS') {
window.location = data.redirectUrl;
}
}
...
这作品真的很好在所有浏览器。但是,有一个在Chrome浏览器的一个问题。它不提供保存密码。
It works really well in all browsers. But there's a problem in Chrome. It doesn't offer to save the password.
当JavaScript是关闭状态,密码被保存,所以这个问题是绝对重定向到一个新的位置。
When JavaScript is turned off, the password is saved, so the problem is definitely in redirection to a new location.
我该如何解决呢?
推荐答案
我已经找到了一个肮脏的解决这一问题,通过将一种无形的iframe的形式指定给它:
I have found a dirty workaround for this problem, by inserting an invisible iframe and targeting the form to it:
<iframe src="/blank.html" id="loginTarget" name="loginTarget" style="display:none">
</iframe>
<form id="loginForm" action="/blank.html" method="post" target="loginTarget"></form>
相应的JavaScript:
The corresponding JavaScript:
$('#loginForm').submit(function () {
$.post('/login', $(this).serialize(), function (data) {
if (data.status == 'SUCCESS') {
window.location = data.redirectUrl;
}
})
})
诀窍是,有做实际上是两个请求。首先,表单被提交到/blank.html,这将被服务器忽略,但这种触发密码保存在浏览器对话框。此外,我们做一个Ajax请求,并提交真实的形式/登录。自从第一次请求的目标是一个隐形的iframe页面不刷新。
The trick is, that there are really two requests made. First the form gets submitted to /blank.html, which will be ignored by the server, but this triggers the password save dialog in Chrome. Additionally we make an ajax request and submit the real form to /login. Since the target of the first request is an invisible iframe the page doesn't refresh.
这当然是更有用的,如果你不想重定向到另一页。如果你想反正重定向更改action属性是一个更好的解决方案。
This is of course more useful if you don't want to redirect to another page. If you want to redirect anyway changing the action attribute is a better solution.
编辑:
下面是一个简单的的jsfiddle 的版本。相反,在评论区要求,没有需要重新加载页面,它似乎工作非常可靠。我用Chrome和Linux上使用铬测试它在Win XP。
Here is a simple JSFiddle version of it. Contrary to claims in the comment section, there is no reload of the page needed and it seems to work very reliably. I tested it on Win XP with Chrome and on Linux with Chromium.
这篇关于如何让浏览器记住密码AJAX的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!