簡體   English   中英

通過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傳統類型提示。

由於string不是類,因此您無法在函數中“類型提示”它,因此會出現錯誤。

您可以使用其他方法(例如filter_input)檢查是否為字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM