Jquery 复制到剪贴板-Ajax已被拒绝

Jquery 复制到剪贴板-Ajax已被拒绝,jquery,ajax,Jquery,Ajax,我试图将ajax返回的结果复制到剪贴板,但每次我得到“document.execCommand('cut'/'copy')都被拒绝” 我尝试了很少的时间来修复它,但没有成功 $('form').submit(function(e){ e.preventDefault(); $.ajax({ url: $('#test').attr('action'), data: $('#test').serialize(), type: 'post',

我试图将ajax返回的结果复制到剪贴板,但每次我得到“document.execCommand('cut'/'copy')都被拒绝” 我尝试了很少的时间来修复它,但没有成功

$('form').submit(function(e){
    e.preventDefault();
    $.ajax({
      url: $('#test').attr('action'),
      data: $('#test').serialize(),
      type: 'post',
      dataType: 'json',
      success: function(data){
        var $temp = $("<input />");
        $(data.response).append($temp).select();
        document.execCommand("copy");
        $temp.remove();
        console.log(data.response);
      }
    });

  });
$('form')。提交(函数(e){
e、 预防默认值();
$.ajax({
url:$('#test').attr('action'),
数据:$(“#测试”).serialize(),
键入:“post”,
数据类型:“json”,
成功:功能(数据){
变量$temp=$(“”);
$(data.response).append($temp.select();
文件。执行命令(“副本”);
$temp.remove();
console.log(data.response);
}
});
});
修复此行

$(data.resposne).append($temp).select();


身体{
字体:400 16px/1.428控制台;
}
.作为控制台{
断字:断字;
}
钮扣{
左边距:18厘米;
}

完成
//参考 var TA=document.getElementById('TA'); TA.addEventListener('change',函数(e){ copyTextToClipboard(即目标值); }); //定义回调函数 功能copyTextToClipboard(文本){ //创建一个 var textArea=document.createElement('textArea'); //隐藏新的 textArea.style.opacity=0; //将新的附加到 document.body.appendChild(textArea); var str=`${text}`; textArea.value=str; textArea.select(); document.execCommand('copy'); }
我也同样感到困惑。这方面的主线似乎是:

在阅读您和我正在尝试的document.execCommand(“copy”)方法时,我突然想到两个警告:

*)它只允许通过https连接(在我的家庭服务器测试环境中,默认情况下我无法模拟https连接,所以这可能也适用于您)


*)它必须是用户发起的操作的一部分(以防止隐藏副本混淆用户)。“点击”事件是通常给出的示例。您选择绑定的事件form submits可以在函数中调用。那么,也许通过更普遍的访问,它不符合用户发起交互的谱系?

您是在每个浏览器中都会遇到这种错误,还是仅在某些浏览器中会遇到这种错误?仅在Firefox浏览器中
$(data.response).append($temp).select();