-1

I am trying to send an email by the static website via javascript API. Currently, I am able to send an email via post request. On clicking send email button I am redirected to that specific API link. I need help to redirect to my custom link and in the back-end, email shall be sent.

<html>

<script>

function email()
{
$.ajax({
type:"POST",
url:"https://formspree.io/MYEMAIL@SOMEMAIL.COM",
contentType:"application/json; charset=utf-8",
dataType:"json",
success:function(result){
alert(result.d);
console.log(result);
}
}); 

 Location.href = "https://google.com";

}
</script>
<body>
 <textarea name="message" placeholder="Your message"></textarea>
  <button type="submit">Send</button>
</body>
</html>
Hardik Shah
  • 4,042
  • 2
  • 20
  • 41
  • Possible duplicate of [How to send an email from JavaScript](https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript) – Syfer Jul 19 '18 at 23:27

1 Answers1

0

you can call function on button submit

 <html>
    <head>

        <script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>
    <script>

    function email()
    {
    $.ajax({
    type:"POST",
    url:"https://formspree.io/expertteam009@gmail.com",
    contentType:"application/json; charset=utf-8",
    crossDomain: true,
    dataType:"json",
   headers:{
                            'Access-Control-Allow-Origin':'*'
                        },
    success:function(result){
    alert(result.d);
    console.log(result);
     window.Location.href = "https://google.com";
    }
    }); 



   }
    </script></head>
    <body>
     <textarea name="message" placeholder="Your message"></textarea>
      <button type="submit" onclick="email()">Send</button>
    </body>
    </html>
sunil
  • 31
  • 5
  • email.html:1 Failed to load https://formspree.io/expertteam009@gmail.com: Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response. following error is in console – Ultimate Hero Jul 19 '18 at 11:58
  • use dataType:"jsonp" , if you facing cross domain issue then you can use this in ajax – sunil Oct 12 '18 at 05:53