我使用这个代码:
$.post('contact.php', {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val()
}, function(data) {
if (data == 'Message sent!') {
alert('Success') ;
}
});
PHP文件返回"Message sent!",但出现500内部服务器错误。我该如何解决这个问题?谢谢!
这是因为在回显"Message Sent"后,php文件会查找在服务器上找不到的东西,如favicon.ico
或任何其他文件可能是js
或css
。因此,为了去除这种使用exit;
后的echoing
类似,
echo 'Message Sent!';
exit;
内部服务器错误消息表示一般情况下有问题。
大多数时候,"错误"意味着页面或网站的编程有问题,但问题肯定存在于用户端。source
php代码的第二个echo json成功并在js中验证
PHP
if($some_var){
echo json_encode(array('success'=>true));
}
else{
echo json_encode(array('error'=>$error_nsg));
}
JS
if(data.success){
console.log("success msg");
}
elseif (data.error){
console.log("error msg");
}