【问题标题】:Php script works locally but on ajax call I get 500 Internal Server ErrorPhp 脚本在本地工作,但在 ajax 调用上我得到 500 Internal Server Error
【发布时间】:2017-08-31 22:12:30
【问题描述】:

我有一个使用 SendGrid 发送电子邮件的 php 脚本。如果我摆脱 $_POST 命令,改为手动设置值并在本地运行(从 cmd)它可以工作。但是当尝试使用 ajax 提交表单时,我收到 500 内部服务器错误。

这里是php代码

<?php require 'vendor/autoload.php';
header("Access-Control-Allow-Origin: *");

if(!(isset($_POST['email']) && isset($_POST['messageType']) && isset($_POST['projectName']) && isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['message']))){
    echo "Unable to process request";
    return;
}

$email = $_POST['email'];
$type = $_POST['messageType'];
$project = $_POST['projectName'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$message = $_POST['message'];

$from = new SendGrid\Email($firstName . " " . $lastName, $email);
$subject = $type == "Message" ? "www.teodorvecerdi.me | " . $type : "www.teodorvecerdi.me | " . $type . " | " . $project;
$to = new SendGrid\Email("Teodor Vecerdi", "teodor.vecerdi@gmail.com");

$content2 = "<table><tr><td>Name</td><td>$firstName $lastName</td></tr><tr><td>Email</td><td>$email</td></tr>";
if($project != "I didn't select 'Suggestion for project'")
    $content2 .= "<tr><td>Project</td><td>$project</td></tr>";
$content2 .= "<tr><td>Message</td><td>$message</td></tr></table>";

$content = new SendGrid\Content("text/html", $content2);
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = 'API KEY';
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();`

这里是 AJAX 调用

$('#SendMessageForm').on("submit", function (e) {
   e.preventDefault();
   var data = $(this).serialize();
   $.ajax({
      url: 'https://sendemail-teodorvecerdi.herokuapp.com',
       type: 'POST',
       data: data,
       success: function (response) {
           console.log(response);
           console.log("Success");
       },
       fail: function (response) {
           console.log(response);
           console.log("Failure");
       }
   });
});

编辑:我在 heroku 上托管脚本

这是我提交表单时在日志中显示的内容:

2017-08-[web.1]: [31-Aug-2017 22:22:06 UTC] PHP Fatal error: Uncaught Error: 
Call to undefined function SendGrid\mb_convert_encoding() in /app/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php:745
[web.1]: Stack trace: 
[web.1]: #0 /app/index.php(36): SendGrid\Content->__construct('text/html', '<table style='b...')
[web.1]: #1 {main} [web.1]: thrown in /app/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php on line 745

【问题讨论】:

  • 你试过检查php错误日志吗?
  • 在哪里可以找到它?
  • 这取决于您的托管(配置)
  • 我已经更新了答案。我正在使用heroku
  • 这是我提交表单时日志中显示的内容:2017-08-[web.1]: [31-Aug-2017 22:22:06 UTC] PHP Fatal error: Uncaught Error: Call to undefined function SendGrid\mb_convert_encoding() in /app/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php:745 [web.1]: Stack trace: [web.1]: #0 /app/index.php(36): SendGrid\Content-&gt;__construct('text/html', '&lt;table style='b...') [web.1]: #1 {main} [web.1]: thrown in /app/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php on line 745

标签: php ajax heroku


【解决方案1】:

我终于解决了这个问题,我所要做的就是像@Neodan 所说的那样在作曲家中启用 mbstring。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    相关资源
    最近更新 更多