Копировать в буфер обмена - отказано в Ajax - PullRequest
0 голосов
/ 03 сентября 2018

Я пытаюсь скопировать в буфер обмена возвращаемый результат из ajax, но каждый раз, когда я получаю «document.execCommand (« вырезать »/« копировать »), было отказано» Я пытаюсь исправить это, но безуспешно

$('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);
      }
    });

  });

1 Ответ

0 голосов
/ 03 сентября 2018

Исправить эту строку

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

до

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

<!DOCTYPE html>
<html>

<head>
  <style>
    body {
      font: 400 16px/1.428 Consolas;
    }
    .as-console {
      word-break: break-word;
    }
    
    button {
      margin-left: 18ch;
    }
  </style>
</head>

<body>
  <textarea id='TA'></textarea>
  <br/>
  <button>DONE</button>
  <br/>

  <script>
    // Reference the <textarea>
    var TA = document.getElementById('TA');


    TA.addEventListener('change', function(e) {


      copyTextToClipboard(e.target.value);
    });

    // Define callback function
    function copyTextToClipboard(text) {

      // Create a <textarea>
      var textArea = document.createElement('textarea');
      // Hide new <textarea>
      textArea.style.opacity = 0;

      // Append new <textarea> to the <form>
      document.body.appendChild(textArea);

      var str = `${text}`;

      textArea.value = str;

      textArea.select();


      document.execCommand('copy');

    }
  </script>
</body>

</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...