执行ajax请求时出现500内部服务器错误

时间:2013-05-22 18:42:06

标签: php jquery ajax

我在调查我的服务器上尝试执行AJAX请求(我正在做“PUT”/“GET”)时遇到的500内部服务器错误时遇到了问题。

本地运行没有任何问题,它会响应,但在我上传内容后,它没有,好像文件/文件夹不在那里。

自从我上次检查以来,host正在Apache上运行至少版本为5.3.0的PHP。我在尝试使用页脚简报订阅时收到错误。

执行AJAX时请求的索引文件如下所示:

<?php

/*
 * Import PHPMailer Class
*/

require("phpmailer/class.phpmailer.php");


/*
 * Decode JSON Data
*/

$request = json_decode(file_get_contents("php://input"), true);


/*
 * Check Valid Email Address
*/

if (!filter_var($request["Email"], FILTER_VALIDATE_EMAIL)) {

    echo json_encode([
        "error" => true,
        "message"=> "You must enter a valid email address"
    ]);

    return false;
};


/*
 * Instantiate PHPMailer Class
*/

$mailer = new PHPMailer();


/*
 * Set Reply Settings
*/

$reply_email        = "no-reply@barbershoppen.dk";
$reply_name         = "Barber Shoppen";


/*
 * Specific PHPMailer Settings
*/

$mailer->IsSMTP();                                  /* SMTP Usage */
$mailer->SMTPAuth   = true;                         /* SMTP Authentication */
$mailer->SMTPSecure = "ssl";                        /* Sets Servier Prefix */
$mailer->Host       = "smtp.gmail.com";             /* SMTP Server */
$mailer->Port       = 465;                          /* SMTP Port */

$mailer->Username   = "rolandeveloper@gmail.com";   /* SMTP Account Username */
$mailer->Password   = "333333333";           /* SMTP Account Password */


/*
 * Email Settings
*/

$mailer->SetFrom($reply_email, $reply_name);
$mailer->AddReplyTo($reply_email, $reply_name);
$mailer->AddAddress($request["Email"]);

$mailer->Subject    = "Barber Shoppen [ Confirmation Email ]";
$mailer->Body       = "You have successfully subscribed to our newsletter";

$mailer->isHTML(true);


/*
 * Send Email
*/

if($mailer->Send()) {

    echo json_encode([
        "error" => false,
        "message"=> "You have successfully subscribed"
    ]);

} else {

    echo json_encode([
        "error" => true,
        "message"=> $mailer->ErrorInfo
    ]);

};
?>

我会感谢一些帮助或一些指示,我应该指示并解决此错误。

1 个答案:

答案 0 :(得分:4)

您希望将PHP样式数组而不是javascript样式数组传入json_encode:

if (!filter_var($request["Email"], FILTER_VALIDATE_EMAIL)) {

    echo json_encode(array(
        "error" => true,
        "message"=> "You must enter a valid email address"
    ));

    return false;
};

并且:

if($mailer->Send()) {

    echo json_encode(array(
        "error" => false,
        "message"=> "You have successfully subscribed"
    ));

} else {

    echo json_encode(array(
        "error" => true,
        "message"=> $mailer->ErrorInfo
    ));

};