[英]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文档中阅读:
类型提示只能是
object
和array
(自PHP 5.1起)类型。 不支持使用int
和string
传统类型提示。
由于string不是类,因此您无法在函数中“类型提示”它,因此会出现错误。
您可以使用其他方法(例如filter_input)检查是否为字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.