'copytoclipboard working on localhost but not on server

I have written script to copy the string to the clipboard and it is working fine on localhost but not working on server. see the image I have attached

 async function copyTextToClipboard(text) {
    console.log(text, "copiedText");
    if ("clipboard" in navigator) {
      return await navigator.clipboard.writeText(text);
    } else {
      return document.execCommand("copy", true, text);
    }
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source