gpt4 book ai didi

jquery - NotAllowedError : Must be handling a user gesture to perform a share request. navigator.share

转载 作者:行者123 更新时间:2023-12-03 14:55:52 25 4
gpt4 key购买 nike

我有一个简单的页面,其中包含我通过 AJAX 请求处理的一些详细信息。
在 AJAX 调用成功回调时,我试图触发 navigator.share这给了我一个错误:

NotAllowedError: Must be handling a user gesture to perform a share request.



一些示例代码如下所示:
$("#form-button").on('click', function (event) {
var ele = $(this);
event.preventDefault();
$.ajax({
// ...
dataType: "json",
data: { "foo": "bar" },
success: function (response) {
if (navigator.share) {
navigator.share({
title: response.text,
text: response.text,
url: response.href
}).then(function () {
alert('Successful share')
}).catch(function (error) {
alert('Error sharing: ' + error);
});
}
},
});
});

武亭来自 Google developer site状态:

... you can only invoke the API in response to a user action, such as a click (e.g., you can't call navigator.share as part of the page load)



如果我没有 AJAX 调用并直接触发 navigator.share,它可以工作在点击事件下,但不是当我把它放在 AJAX 回调中时。我希望 API 会检查事件链。

我必须使用哪些替代方法才能使其发挥作用?我试过保留一个虚拟按钮并触发点击。

最佳答案

Chromium 目前似乎显示了拒绝数据类型的错误错误消息,例如尝试共享 pdf 文件;也不允许共享 JSON 文件。
Chromium 拒绝“共享”的其他情况也可能会给您同样的错误消息。例如,在我的情况下,我在本地测试(file://...)或使用“http”而不是“https”。
请参阅我在其中找到此信息的这条推文:
https://twitter.com/daviddalbusco/status/1344641231184396288
并且允许的文件扩展名列表在那里:
https://docs.google.com/document/d/1tKPkHA5nnJtmh2TgqWmGSREUzXgMUFDL6yMdVZHqUsg/
[编辑]
我确认相同的代码在移动 Chrome 上运行良好,但在桌面 Chrome 89 上失败,如下所示:

  • 如果我在调试器中选中“捕获异常时暂停”,我会收到错误消息:“DOMException:无法在“导航器”上执行“共享”:必须处理用户手势才能执行共享请求。”
  • 如果我不检查“捕获异常时暂停”,我会得到几秒钟的“延迟”,然后 promise 会失败并出现错误“DOMException:共享已取消”。

  • 简而言之,我的回答是:“Chrome 桌面 89 还没有为 Web Share API 做好准备”(如果你尝试,它会给你误导性的错误消息)。

    关于jquery - NotAllowedError : Must be handling a user gesture to perform a share request. navigator.share,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56136692/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com