Php 向ajax报告错误
下面的代码通过ajax将表单数据发布到upload.php。在php文件中,服务器检查上传的文件大小/格式等。我如何告诉ajax这是否失败并停止显示成功消息Php 向ajax报告错误,php,ajax,post,Php,Ajax,Post,下面的代码通过ajax将表单数据发布到upload.php。在php文件中,服务器检查上传的文件大小/格式等。我如何告诉ajax这是否失败并停止显示成功消息 $.ajax({ type: 'POST', url: 'upload.php', data: dataString, success: function() { $('#getintouch .alert-error').slideUp(); $('#getintouch .
$.ajax({
type: 'POST',
url: 'upload.php',
data: dataString,
success: function() {
$('#getintouch .alert-error').slideUp();
$('#getintouch .alert-success').slideDown();
$('#submit').data('loading-text', 'Sent');
$('#submit').button('loading');
}
});
这可以通过两种方式实现:
success: function( data ) {
if ( !data.success ) //if JSON
{
// do error message
return;
}
http_response_code( 500 );
编辑-使用前端上的错误参数进行处理:
error: function(){...},
success: function(){...},
只需添加:如果是5xx HTTP代码,您应该使用“error”AJAX回调:我使用了方法2,但是尽管将响应代码设置为500,并将脚本设置为die(),成功事件仍然被触发;啊哈!http_响应_代码需要PHP5.4,我使用的是5.3。我使用了下面的代码来代替标题(“:”,true,500);