如何让 Chrome 记住 AJAX 表单的密码?
我在登录页面上使用 AJAX 进行快速输入验证。如果一切正确,用户将被重定向。
这是代码:
$(form).submit(function () {
$.post($(this).attr('action'), $(this).serialize(), function (data) {
if (data.status == 'SUCCESS') {
window.location = data.redirectUrl;
}
}
...
它在所有浏览器中都运行良好。但 Chrome 有一个问题。它不提供保存密码的功能。
当 JavaScript 关闭时,密码会被保存,因此问题肯定是重定向到新位置。
我该如何解决这个问题?
I'm using AJAX for fast input validation on my login page. If everything is correct, the user is redirected.
Here's the code:
$(form).submit(function () {
$.post($(this).attr('action'), $(this).serialize(), function (data) {
if (data.status == 'SUCCESS') {
window.location = data.redirectUrl;
}
}
...
It works really well in all browsers. But there's a problem in Chrome. It doesn't offer to save the password.
When JavaScript is turned off, the password is saved, so the problem is definitely in redirection to a new location.
How can I fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我找到了解决此问题的一个肮脏的解决方法,即插入一个不可见的 iframe 并将表单定位到它:
相应的 JavaScript:
诀窍是,实际上发出了两个请求。首先,表单被提交到 /blank.html,这将被服务器忽略,但这会触发 Chrome 中的密码保存对话框。此外,我们发出 ajax 请求并将真实表单提交到 /login。由于第一个请求的目标是不可见的 iframe,因此页面不会刷新。
如果您不想重定向到另一个页面,这当然更有用。如果您想重定向,更改操作属性是更好的解决方案。
编辑:
这是一个简单的JSFiddle版本。与评论部分中的说法相反,不需要重新加载页面,而且它似乎工作非常可靠。我在带有 Chrome 的 Win XP 和带有 Chromium 的 Linux 上测试了它。
I have found a dirty workaround for this problem, by inserting an invisible iframe and targeting the form to it:
The corresponding JavaScript:
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.
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.
Edit:
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.
您是否可以将表单的
action
值更改为data.redirectUrl
并让表单照常提交?这应该会触发浏览器提示保存用户名和密码。Are you able to change the form's
action
value todata.redirectUrl
and let the form submit as usual? This should trigger the browser's prompt to save the username and password.请阅读此处 - 为什么 Chrome 无法识别此登录表单? .
重要的评论是:
因此,您可以考虑删除 Ajax,只让表单发布来登录,这可能是未启用 JavaScript 的用户使用您的表单登录的唯一方法。
Have a read here - why doesn't chrome recognize this login form? .
The important comment is:
So you could maybe consider removing the Ajax and just letting the Form post to login, this will probably be the only way for Users that do not have JavaScript enabled to login with your form too.
我已经用这种方式修复了它:
和 JavaScript:
I have fixed it using this way:
And the JavaScript:
就我而言,Chrome 不记得密码,因为在一种表单中有两种不同的密码类型输入(在一种表单中创建/登录)。我的例子中的问题是通过使用 JavaScript 操作删除密码类型的输入之一来解决的,以便浏览器可以决定哪些提交的字段包含凭据数据。
In my case Chrome didn't remember password because there were two different inputs of type password in one form (create/login in one form). The issue in my case was solved by using javascript manipulation of removing one of the input of type password so that browser could decide which submitted fields contains credential data.
因此,现在在 2019 年,我们可以使用凭据管理 API 来实现此目的。
注意:这是实验性 API!
检查一下:https://developers.google.com/web /fundamentals/security/credential-management/save-forms
基本代码:
So now in 2019 we can do this using Credential Management API.
NOTE: it is experimental API!
Check this: https://developers.google.com/web/fundamentals/security/credential-management/save-forms
Essential code:
我发现用户名和密码输入字段必须设置名称标签才能让 Chrome 提供保存密码的功能。 此主题是关于简单表单的,但同样修复了我的 jquery AJAX 表单提交。
I found that the username and password input fields must have the name tag set in order for Chrome to offer to save the password. This thread is about simple forms, but the same fixed my jquery AJAX form submission.
从昨天(2013 年 10 月 7 日)开始,Chrome 28 现在无需任何技巧即可实现。看来他们解决了...
From yesterday, 10/07/2013, Chrome 28, it's now possible without any trick. Seems they fixed that...
我遇到了同样的问题 - 如何强制浏览器存储 AJAX 请求的用户凭据。请在此处查看我的答案 - https://stackoverflow.com/a/64140352/994289。
希望有帮助。至少它对我来说在 Windows 上的 Chrome 上运行得很好。
I had the same issue - how to force browser to store user credentials for AJAX requests. Please see my answer here - https://stackoverflow.com/a/64140352/994289.
Hope it helps. At least it worked fine for me on Chrome on Windows.