我有一个简短的问题。当我按下使用 ajax 请求将数据放入剪贴板的按钮时,我想向 iOS 设备上的剪贴板添加文本。
在我的桌面和 Android 设备上使用浏览器时,它运行得很好,但在我的 iOS 设备上却出现问题。
$.ajax({
type: "GET",
url: "missing_string.php",
data: getAttributes($(this)),
success: function(data) {
if (navigator.clipboard) {
navigator.clipboard.writeText(data)
.then(() => {
alert("SUCCESS");
})
.catch(err => {
alert(err);
console.log('Something went wrong', err);
});
所以我收到以下错误消息:
NotAllowedError:当前上下文中的用户代理或平台不允许该请求,可能是因为用户拒绝了权限。
如何将我的文本复制到剪贴板?我使用iOS 13.5和13.4应该支持剪贴板API。
https://webkit.org/blog/10247/new-webkit-features-in-safari-13-1/
Async Clipboard API
WebKit brings the Async Clipboard API to this release of Safari...
you should read it
TLDR: 该实现可通过
navigator.clipboard
API 实现,该 API 必须在用户事件中调用
您尝试通过 API 调用而不是在用户交互的上下文中复制到剪贴板。这是不允许的。
对我来说,问题是我没有在“安全上下文”(HTTPS)中运行网络应用程序。在控制台中尝试
window.isSecureContext
。