【问题标题】:500 internal server error when doing ajax request执行 ajax 请求时出现 500 内部服务器错误
【发布时间】:2013-05-22 18:42:06
【问题描述】:

我无法调查在我的服务器上尝试执行 AJAX 请求(我正在执行“PUT”/“GET”)时遇到的 500 Internal Server Error。

在本地它运行没有任何问题并且它响应,但是在我将内容上传到服务器后它没有,好像文件/文件夹不存在一样。

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

执行 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
    ]);

};
?>

我将不胜感激一些帮助或一些指示,我应该朝哪个方向前进并修复此错误。

【问题讨论】:

  • 您不只是在这里发布您的 gmail 帐户数据吗?希望在您编辑问题之前,坏人不会窃取您的帐户...
  • 服务器日志有错误吗? “500 Internal Server Error”表示存在。
  • 我想您现在知道内部错误是因为将错误报告设置为关闭。所以请运行ini_set('error_reporting','on'); 来查看确切的错误。
  • 是的......这是一个开发帐户......很多人都有它,但我还是删除了通行证:)
  • 查看apache错误日志:tail /var/log/apache2/error.log

标签: php jquery ajax


【解决方案1】:

您想将 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
    ));

};

【讨论】:

  • echo json_encode([ "error" =&gt; true, "message"=&gt; "You must enter a valid email address" ]); 可以在 php 5.4 中使用数组短语法工作,但在 5.3 中不行!
  • 我没有意识到它被添加到 5.4,尽管我喜欢向后兼容的风格。 :-)
  • 就是这样......但奇怪的是我得到了 500 并且没有描述,如果能得到一个小的描述告诉我我很愚蠢而且我没有使用正确的语法
  • 始终检查错误日志。当浏览器抛出 500 时,它并不总是提供任何其他信息,具体取决于 error_level。不过,它通常在日志中。
  • 我必须启用错误报告,但我很难绕过 Plesk ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 2017-11-25
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多