如何让 Chrome 记住 AJAX 表单的密码?

发布于 2024-10-27 04:08:53 字数 414 浏览 1 评论 0原文

我在登录页面上使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

甜味拾荒者 2024-11-03 04:08:53

我找到了解决此问题的一个肮脏的解决方法,即插入一个不可见的 iframe 并将表单定位到它:

<iframe src="/blank.html" id="loginTarget" name="loginTarget" style="display:none">
</iframe>

<form id="loginForm" action="/blank.html" method="post" target="loginTarget"></form>

相应的 JavaScript:

$('#loginForm').submit(function () {
    $.post('/login', $(this).serialize(), function (data) {
        if (data.status == 'SUCCESS') {
            window.location = data.redirectUrl;
        }
    })
})

诀窍是,实际上发出了两个请求。首先,表单被提交到 /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:

<iframe src="/blank.html" id="loginTarget" name="loginTarget" style="display:none">
</iframe>

<form id="loginForm" action="/blank.html" method="post" target="loginTarget"></form>

The corresponding JavaScript:

$('#loginForm').submit(function () {
    $.post('/login', $(this).serialize(), function (data) {
        if (data.status == 'SUCCESS') {
            window.location = data.redirectUrl;
        }
    })
})

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.

ゞ花落谁相伴 2024-11-03 04:08:53

您是否可以将表单的 action 值更改为 data.redirectUrl 并让表单照常提交?这应该会触发浏览器提示保存用户名和密码。

$(form).submit(function () {
    $.post($(this).attr('action'), $(this).serialize(), function (data) {
        if (data.status == 'SUCCESS') {
            $("form#name").attr('action', data.redirectUrl);
        }
    }
...

Are you able to change the form's action value to data.redirectUrl and let the form submit as usual? This should trigger the browser's prompt to save the username and password.

$(form).submit(function () {
    $.post($(this).attr('action'), $(this).serialize(), function (data) {
        if (data.status == 'SUCCESS') {
            $("form#name").attr('action', data.redirectUrl);
        }
    }
...
热情消退 2024-11-03 04:08:53

请阅读此处 - 为什么 Chrome 无法识别此登录表单? .

重要的评论是:

是的,删除后它不起作用
返回假。你需要重写
你的代码。 Chrome 不提供
保存非表单中的密码
“提交”作为一项安全功能。如果
您希望保存密码功能
工作,你将不得不放弃
整个奇特的 AJAX 登录。

因此,您可以考虑删除 Ajax,只让表单发布来登录,这可能是未启用 JavaScript 的用户使用您的表单登录的唯一方法。

Have a read here - why doesn't chrome recognize this login form? .

The important comment is:

Yes, it doesn't work when you remove
return false. You will need to rewrite
your code. Chrome does not offer to
save passwords from forms that are not
"submitted" as a security feature. If
you want the Save Password feature to
work, you're going to have to ditch
the whole fancy AJAX login.

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.

只为一人 2024-11-03 04:08:53

我已经用这种方式修复了它:

<form action="/login"></form>

和 JavaScript:

$(form).submit(function () {
   if(-1 !== this.action.indexOf('/login')) {
      var jForm = $(this);
      $.post(this.action, $(this).serialize(), function (data) {
         if (data.status == 'SUCCESS') {

            // change the form action to another url
            jForm[0].action = data.redirectUrl;

            // and resubmit -> so, no AJAX will be used 
            // and function will return true
            jForm.submit();
         }
      });
      return false;
   }
}

I have fixed it using this way:

<form action="/login"></form>

And the JavaScript:

$(form).submit(function () {
   if(-1 !== this.action.indexOf('/login')) {
      var jForm = $(this);
      $.post(this.action, $(this).serialize(), function (data) {
         if (data.status == 'SUCCESS') {

            // change the form action to another url
            jForm[0].action = data.redirectUrl;

            // and resubmit -> so, no AJAX will be used 
            // and function will return true
            jForm.submit();
         }
      });
      return false;
   }
}
各自安好 2024-11-03 04:08:53

就我而言,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.

怂人 2024-11-03 04:08:53

因此,现在在 2019 年,我们可以使用凭据管理 API 来实现此目的。

注意:这是实验性 API!
检查一下:https://developers.google.com/web /fundamentals/security/credential-management/save-forms

基本代码:

function onSubmit() {
    makeAjaxLoginRequest()
    .then((status) => {
        if (status.success) {
            if (window.PasswordCredential) {
                var cr = new PasswordCredential({ id: login, password: password });
                return navigator.credentials.store(cr);
            } else {
                return Promise.resolve();
            }
        }
    }).then(
        () => { // redirect || hide login window, etc... },
        () => { // show errors, etc... }
    )
}

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:

function onSubmit() {
    makeAjaxLoginRequest()
    .then((status) => {
        if (status.success) {
            if (window.PasswordCredential) {
                var cr = new PasswordCredential({ id: login, password: password });
                return navigator.credentials.store(cr);
            } else {
                return Promise.resolve();
            }
        }
    }).then(
        () => { // redirect || hide login window, etc... },
        () => { // show errors, etc... }
    )
}
晨与橙与城 2024-11-03 04:08:53

我发现用户名和密码输入字段必须设置名称标签才能让 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.

亽野灬性zι浪 2024-11-03 04:08:53

从昨天(2013 年 10 月 7 日)开始,Chrome 28 现在无需任何技巧即可实现。看来他们解决了...

From yesterday, 10/07/2013, Chrome 28, it's now possible without any trick. Seems they fixed that...

夜巴黎 2024-11-03 04:08:53

我遇到了同样的问题 - 如何强制浏览器存储 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文