发出AJAX请求时如何解决“ JSON解析错误:意外的标识符数组”

时间:2019-09-11 13:15:54

标签: javascript php html ajax

我正在设置联系表格验证,并且我需要来自php脚本'sendEmail.php'的$ msg变量。实际的邮件系统正在运行,脚本正在从index.php接收表单输入,并且正在运行(成功发送电子邮件)... 问题是“成功:函数(数据)”不起作用,并且已经连续几天试图找出我在做什么错

我尝试更改为$ .post函数,将许多类似的示例进行比较,并使用'error:'函数进行调试...

错误显示在控制台'console.log(errorThrown)'

我正在使用Safari,并且可以从sendEmail.php的XHRs文件夹中获得此文件: 输出:

array(3) {
  ["name"]=>
  string(12) "asfasfasfasf"
  ["email"]=>
  string(9) "asfasfasf"
  ["message"]=>
  string(0) ""
}
{"code":404,"msg":"Please fill in all fields"}
// AJAX Call
$.ajax({
  type: "POST",
  url: "php/sendEmail.php",
  dataType: "json",
  data: {
    name: name,
    email: email,
    message: message
  },
  success: function(data) {

  if (data.code == '200') {
    $('.alert-success').html(data.msg);
    $('.alert-success').css('display', 'block');
  } else {
    $('.alert-danger').html(data.msg);
    $('.alert-danger').css('display', 'block');
  }

  },error: function(jqXHR, textStatus, errorThrown) {
     console.log(errorThrown);
  }
});

// Error Message
$msg = '';

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {

    // Get Form Data
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    var_dump($_POST);

    // Verifications
    if (!empty($name) && !empty($email) && !empty($message)) {
        // Name Check
        if (strlen($name) <= 4 || strlen($name) >= 30) {
            $msg = 'Please type a name between 4 and 30 characters';
        } else {
            // Email Check
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $msg = 'Please provide a valid email';
            } else {
                // Message Check
                if (strlen($message) < 10) {
                    $msg = 'Please provide a message superior to 10           characters';
                } else {
                    // Send Email
                        $msg = 'Email was sent successfully...';
                        echo json_encode(['code' => 200, 'msg' => $msg]);
                        exit;
                    } catch (Exception $e) {
                        $msg = 'Error: Email was not sent...';
                    }
                }
            }
        }
    } else {
        $msg = 'Please fill in all fields';
    }
}

echo json_encode(['code' => 404, 'msg' => $msg]);

``

1 个答案:

答案 0 :(得分:0)

检查代码并执行以下操作

array(3) {
  ["name"]=>
  string(12) "asfasfasfasf"
  ["email"]=>
  string(9) "asfasfasf"
  ["message"]=>
  string(0) ""
}
{"code":404,"msg":"Please fill in all fields"}

我意识到此时您的代码中有一个var_dump:

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {

    // Get Form Data
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    var_dump($_POST); // <---- Remove this and everything should be okay

希望它会帮助