通过AJAX在简单表单POST上收到500错误 - 不确定出了什么问题


Getting 500 error on simple form POST via AJAX -- not sure what's wrong?

我不明白为什么在这个简单的基于 AJAX 的表单 POST 到 PHP 上收到 500 错误......介意看看吗?这可能是一些简单的事情,我只是没有抓住,但我现在正在挠头。

.JS:

    $form.submit(function(e) {
        e.preventDefault();
        var formData = $form.serialize();
        $.ajax({
            url: $form.attr('action'),
            type: 'POST',
            data: formData
        }).done(function(res) {
            console.dir(res);
            alert('success!'); // update status text
            $form[0].reset(); // reset the form
            $form.find('input[type="submit"]').attr('disabled', 'disabled');
        }).fail(function(data) {
            alert('error');
        });
    });

.PHP:

if($_SERVER["REQUEST_METHOD"] == "POST") {
  if( !empty($_POST['captcha']) ) {
    processForm( $_POST['formName'] );
  } else {
    http_response_code(401);
    echo "Unauthorized.";
  }
} else {
    // not valid. ignore
    http_response_code(400);
    echo "Bad Request.";
}

PHP: processForm():

function processForm(string $type) {
    if ($type == 'contact') {
        http_response_code(200);
        echo "contact form";
    }
    if ($type == 'appointmentRequest') {
        http_response_code(200);
        echo "appointment form";
    }
    // send the email
    // return confirmation / failure
    // die
}

我刚刚收到 500 错误。我确定请求指向正确的位置。

抱歉,正如您在 PHP 5 文档中看到的那样:

类型提示只能是objectarray(从 PHP 5.1 开始)类型。不支持带有intstring的传统类型提示。

由于字符串不是一个类,因此您无法在函数中"类型提示"它,因此会出现错误。

您可以使用其他方法检查它是否是字符串,例如filter_input