This question already has answers here:
Uncaught ReferenceError: $ is not defined?
(37个答案)
三年前关闭。
我想发送一封电子邮件,只要我点击html按钮。为了完成这项任务,我编写了以下代码
HTML代码:
<button onclick="sendEmail()">Send Email</button>
<p id="mailStatus"></p>

Java脚本代码:
function sendEmail()
{
     $.ajax({
           url: "mail.php",
           type: "POST",
           success: function(response) {
               if (!response) {
                    alert("Something went wrong. Please try again");
                    return;
               }

               var parsedJSON = eval('('+response+')');

               // If there's an error, display it.
               if(parsedJSON.Error) {
                  // Handle session timeout.
                  if (parsedJSON.Error == "Timeout") {
                       alert("Session timed out. Please login again.");
                       window.location.reload();
                   }
                }
               document.getElementById('mailStatus').innerHTML = "Email Sent successfully";
            }
     });
}

问题是,当我点击Send Email按钮时,我收到一条错误消息
"Uncaught ReferenceError: $ is not defined"
有人能帮忙吗??

最佳答案

因为$.ajax是jQuery的函数,所以需要在文件中添加jQuery。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

sendMail函数上方添加这一行。

10-06 12:01