无法弄清楚为什么AJAX没有触发

时间:2016-09-05 16:15:14

标签: php ajax

我正在旋转我的车轮试图弄清楚我的代码中的错误。由" .func2"标识的功能触发就好了,做了应该做的事情。然而,当" .func1"函数正在被触发(那个window.alert'我在这里'正在出现),那个" .func1"中的AJAX调用没有触发。 " func2_email.php ...."没有被援引。

这两个函数都是从同一个PHP页面调用的。

为什么会发生这种情况?

    <script type="text/javascript">
$(function () {

     $(“.func1").on("click", function () {
        var fromEmail = $("#fromEmail").val();
        var toEmail = $("#to_Email").val();
        var transid = $("#refTransid").val();
        var repid = $("#initiatedby").val();
        var feed = $("#refFeed").val();
        var message = $("#to_message").val();
        var toName = $("#to_Name").val();
        var shareid = $("#shareid").val();

        //  window.alert(“I am here”);

        $.ajax({
            type: 'GET',
            url: “func2_email.php?fromEmail=" + fromEmail + "&toEmail=" + toEmail + "&toName=" + toName + "&message=" + message + "&shareId=" + shareid + "&transid=" + transid + "&repid=" + repid + "&feed=" + feed,
            dataType: "html",
            success: function (data) {
                $("#msg").html("<p class='successmsg'>" + toName + " has been notified");
            }               
        });
    });     

     $(“.func2").on("click", function () {

        var email = $(this).attr("email");
        var userid = $(this).attr("userid");
        var transid = $("#transid").val();
        var repid = $("#repid").val();
        var feed = $("#feed").val();

        var name = $(this).text();

        $.ajax({
            type: 'GET',
            url: “func1_email.php?userid=" + userid + "&feed=" + feed + "&transid=" + transid + "&repid=" + repid + "&email=" + email,
            dataType: "html",
            success: function (data) {
                var scrollTop    = $(document).scrollTop();
                var top          = parseInt(scrollTop+260);
                $("#msg").html("<p class='successmsg'>" + name + " has been notified </p>");

            }
        });
    });         

});
</script>

func2_email.php的代码

    <?php
session_start();
require_once("classes/autoload.php");
$db     = new Database();

//$fromEmail  = $_GET["fromEmail"];     // If client wants to use their internal user names
$fromEmail = "admin@mydomain.com";  //Central email address
$toEmail  = $_GET["toEmail"];
$toName  = $_GET["toName"];
$message = $_GET["message"];
$shareId = $_GET["shareId"];
$transid  = $_GET["transid"];
$repid  = $_GET["repid"];
$feed  = $_GET["feed"];

$host = $db->getDbHost();
$companyname = $db->getFieldValue("companyname", "apphost", $host, "appsettings");

$body   = "Dear ".$toName."," ."</br>".$message."</br>Please click on the link below to view the page”."displaytransaction.php?transid=".$transid."&id=".$shareId."&requestor=".$repid."&feed=".$feed."<br></br>Sincerely, </br>Customer Operations</br>".$companyname;



$mail = new PHPMailer(true);  // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host       = "smtp.mailgun.org"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 2020;                    /
$mail->Username   = “postmaster@mydomain.com"; // SMTP account username
$mail->Password   = "d2e3d568bb45ea649661d86d6a553cf";        // SMTP account password


$mail->AddAddress($email);
$mail->SetFrom($fromEmail);
$mail->Subject = "Requesting you to join a live video sharing session on     transaction #". $transid;
$mail->MsgHTML($body);
$mail->Send();

echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {

echo $ e-&gt; getMessage(); //从其他任何地方感受错误消息!    }

&GT;

1 个答案:

答案 0 :(得分:0)

我已在我的编辑器中检查并尝试在我的本地环境中运行此代码。进行Ajax调用。请尝试一下,然后根据您的需要进行修改。

html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<button class="func1 func2">test button</button>

<script type="text/javascript">
    $(function () {

        $(".func1").on("click", function () {
            var fromEmail = 1;
            var toEmail = 2;
            var transid = 3;
            var repid = 4;
            var feed = 5;
            var message = 6;
            var toName = 7;
            var shareid = 8;


            $.ajax({
                type: 'GET',
                url: "func2_email.php?fromEmail=" + fromEmail + "&toEmail=" + toEmail + "&toName=" + toName + "&message=" + message + "&shareId=" + shareid + "&transid=" + transid + "&repid=" + repid + "&feed=" + feed,
                dataType: "html",
                success: function (data) {
                    $("#msg").html("<p class='successmsg'>" + toName + " has been notified");
                }
            });
        });

        $(".func2").on("click", function () {

            var email = 11;
            var userid = 12;
            var transid = 13;
            var repid = 14;
            var feed = 15;

            var name = 16;

            $.ajax({
                type: 'GET',
                url: "func1_email.php?userid=" + userid + "&feed=" + feed + "&transid=" + transid + "&repid=" + repid + "&email=" + email,
                dataType: "html",
                success: function (data) {
                    var scrollTop = $(document).scrollTop();
                    var top = parseInt(scrollTop + 260);
                    $("#msg").html("<p class='successmsg'>" + name + " has been notified </p>");

                }
            });
        });

    });
</script>
</body>
</html>

func 1

<?php

echo '<h1>FUNC 1</h1>';

func 2

<?php

echo '<h1>FUNC 2</h1>';