0

I am trying call a REST API post function from a script embedded into my html file. The button isn't working for some reason and no online resources have helped me thus far. This html code will be sent via email, and person using outlook with open the email and click the link to call the rest api.

Here is the html code:

<a href="#" onclick="markAsDone();" target="_blank" style="font-size: 16px; font-family: Helvetica, Arial, sans-serif; font-weight: normal; color: #ffffff;    text-decoration: none; display: inline-block;">I am done. &rarr;</a>
<script type="text/javascript">
function markAsDone() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://localhost:1337/UserDone", false);
    xhttp.sendRequestHeader("Content-type", "application/json");
    xhttp.send();
}
</script>

Am I doing something wrong or is outlook not compatible for emails containing html with links like this?

Megan
  • 1,000
  • 1
  • 14
  • 44
  • Possible duplicate of [Is JavaScript supported in an email message?](https://stackoverflow.com/questions/3054315/is-javascript-supported-in-an-email-message) – Igoranze Jun 23 '17 at 18:00
  • Have you looked at the Console in your browser? Seems like you're a victim of [CORS error](https://stackoverflow.com/questions/19325314/how-to-detect-cross-origin-cors-error-vs-other-types-of-errors-for-xmlhttpreq) by the look of the link. Try setting up another link and try again. – FireSarge Jun 23 '17 at 18:00
  • Yes, I've looked at my console, I don't think its CORS error. – Megan Jun 23 '17 at 18:05

1 Answers1

0

I think email clients do not support the use of Javascript. I suggest you to create a normal link in the email, e.g. to http://example.com/email/index.php, and make the API call there.

  • Is there a way I could do this and send string values? – Megan Jun 23 '17 at 18:03
  • Yes, if you would create an URL like this: /?param1=value1&param2=value2, and retrieve those on the serverside with (in case of PHP) a $_GET, you should be able to prefill the link with for example the email address of the user. – Arjen Tienkamp Jun 24 '17 at 17:26
  • Is it possible to not redirect to browser when this is clicked in the email? – AliAvci Oct 29 '22 at 20:44