Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向ajax报告错误_Php_Ajax_Post - Fatal编程技术网

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将表单数据发布到upload.php。在php文件中,服务器检查上传的文件大小/格式等。我如何告诉ajax这是否失败并停止显示成功消息

$.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');
    }
});

这可以通过两种方式实现:

  • 让服务器回显消息,并让JavaScript检查消息

    success: function( data ) {
        if ( !data.success ) //if JSON
        {
            // do error message
            return;
        }
    
  • 更改HTTP响应代码

    http_response_code( 500 );
    
    编辑-使用前端上的错误参数进行处理:

    error:   function(){...},
    success: function(){...}, 
    

  • 只需添加:如果是5xx HTTP代码,您应该使用“error”AJAX回调:我使用了方法2,但是尽管将响应代码设置为500,并将脚本设置为die(),成功事件仍然被触发;啊哈!http_响应_代码需要PHP5.4,我使用的是5.3。我使用了下面的代码来代替标题(“:”,true,500);