javascript - Ajax copy data value to clipboard - Stack Overflow

I need to copy value of data in ajax success function$.ajax({url: 'imagesgetDownloadUrl',da

I need to copy value of data in ajax success function

$.ajax({
    url: 'images/getDownloadUrl/',
    dataType: 'text',
    async: false,
    processData: false,
    contentType: false,
    type: 'POST',
    success: function(data){
    document.execCommand(data);
    }
});

How can i copy value of this variable data to clipboard, because this is not work if i only put execCommand?

I need to copy value of data in ajax success function

$.ajax({
    url: 'images/getDownloadUrl/',
    dataType: 'text',
    async: false,
    processData: false,
    contentType: false,
    type: 'POST',
    success: function(data){
    document.execCommand(data);
    }
});

How can i copy value of this variable data to clipboard, because this is not work if i only put execCommand?

Share Improve this question asked May 31, 2017 at 14:36 alonso05alonso05 1351 gold badge4 silver badges15 bronze badges 4
  • 1 Possible duplicate of How do I copy to the clipboard in JavaScript? – Bernd Strehl Commented May 31, 2017 at 14:40
  • How is this ajax call triggered? The browser must be able to attribute the execCommand to a user trusted event such as "onClick" – bm_i Commented May 31, 2017 at 14:51
  • i found this, but it's not the same problem, because i have problem with ajax – alonso05 Commented May 31, 2017 at 14:52
  • @bm_i it is triggered with event onClick function that contain this ajax – alonso05 Commented May 31, 2017 at 14:53
Add a ment  | 

2 Answers 2

Reset to default 6

You can copy your data to clipboard like that :

$.ajax({
    url: 'images/getDownloadUrl/',
    dataType: 'text',
    async: false,
    processData: false,
    contentType: false,
    type: 'POST',
    success: function(data){
       let copyFrom = document.createElement("textarea");
        document.body.appendChild(copyFrom);
        copyFrom.textContent = data;
        copyFrom.select();
        document.execCommand("copy");
        copyFrom.remove();
    }
});

With async: false, it works for me, but only once. The copy works one time, for the first click on the button which does the ajax call. Using multiple buttons also does not work - only the first click on any one button works.

Copy never works with async: true.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744374643a4571123.html

相关推荐

  • javascript - Ajax copy data value to clipboard - Stack Overflow

    I need to copy value of data in ajax success function$.ajax({url: 'imagesgetDownloadUrl',da

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信