【问题标题】:Sending email via javascript通过 javascript 发送电子邮件
【发布时间】:2018-07-19 11:06:09
【问题描述】:

我正在尝试通过 javascript API 通过静态网站发送电子邮件。目前,我可以通过邮寄请求发送电子邮件。单击发送电子邮件按钮后,我将被重定向到该特定 API 链接。 我需要帮助重定向到我的自定义链接,并在后端发送电子邮件。

<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>

【问题讨论】:

标签: javascript html-email


【解决方案1】:

你可以在按钮提交时调用函数

 <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>

【讨论】:

  • email.html:1 无法加载 formspree.io/expertteam009@gmail.com:请求标头字段 Access-Control-Allow-Origin 在预检响应中被 Access-Control-Allow-Headers 不允许。以下错误在控制台中
  • 使用 dataType:"jsonp" ,如果您面临跨域问题,那么您可以在 ajax 中使用它
猜你喜欢
  • 2012-03-03
  • 2019-01-24
  • 1970-01-01
  • 2017-04-03
  • 2012-01-07
  • 2012-04-27
  • 2016-07-04
  • 2018-09-19
相关资源
最近更新 更多