duanfei8897
duanfei8897
2017-02-09 06:34

如何在ajax错误上显示文件上传错误消息?

I'd like to display file upload error on an ajax alert error message.

This is my ajax:

$.ajax({
        url         : url,
        type        : 'POST',
        cache       : false,
        contentType : false,
        processData : false,
        data        : all_data,
        dataType    : 'JSON',
        success     : function(data)
        {
            if (data.result != 0) {
                toastr.options = {
                    closeButton : false,
                    progressBar : false,
                    showMethod  : 'slideDown',
                    timeOut     : 3000
                };

                toastr.success('UPLOAD SUCCESS', 'Attention...');

                if(activeid != 0){
                    if($("#tl_responder").val() != "" && $("#tl_dept").val() != "" && $("#tl_jawab").val() != ""){
                        toastr.success('MAIL IS SENT', 'ATTENTION...');
                    }
                }

            } else { window.location.href = "<?php print base_url(); ?>complaint/detail?id=" + data.result + "#reloadafteradd"; }

        },
        error: function (jqXHR, textStatus, errorThrown, data)
        {
            toastr.options = {
                closeButton : false,
                progressBar : false,
                showMethod  : 'slideDown',
                timeOut     : 3000
            };
            toastr.error(errorThrown, 'Error...'); //THIS IS THE AJAX ERROR ALERT
           if(!errorThrown){ toastr.error(data.upl_error, 'Error...'); }//This is not working

        }
    });

The AJAX error alert only shows alert when AJAX is not working. I'd like to display the file upload error here.

This is my controller that handle file upload when not uploading:

if ( ! $this->upload->do_upload('file')){

        $response_array = array ('upl_error' => $this->upload->display_errors());
        echo json_encode($response_array);
        //return false;
        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanhao4156 duanhao4156 4年前

    You need to send a flag to specify if file uploaded successfully or not to do this send a success flag from your php code like this:

    PHP CODE

    if ( ! $this->upload->do_upload('file')){
        // file not  uploaded
        $response_array = array ('success'=0,'upl_error' =>   $this->upload->display_errors('', ''));
        echo json_encode($response_array);
    
    }
    else
    {
      // file uploaded
       echo json_encode(array("success"=>1));
    }
    

    And then in your ajax success callback function you need to check that success flag like this

    jQuery CODE

     $.ajax({
            url         : url,
            type        : 'POST',
            cache       : false,
            contentType : false,
            processData : false,
            data        : all_data,
            dataType    : 'JSON',
            success     : function(data)
            {
               // if file upload success
                if (data.success == 1) {
    
                   alert("File uploaed success");
    
                } 
                // file not uploaded
                else { 
                    alert(data.upl_error);
                }
    
            }
    });
    

    I hope it will help you.

    点赞 评论 复制链接分享
  • dpp3047 dpp3047 4年前

    In success function to check response content, or use http_response_code(500) in php script to trigger ajax error function.

    点赞 评论 复制链接分享
  • dpglo66848 dpglo66848 4年前

    The AJAX error alert only shows alert when AJAX is not working. This is working fine because, the case your file is not uploading is not an ajax error, its a normal code flow.

    To handle it, you have to return some status to manage it in success of ajax like:

    success: function(response)
    {
        if(response == 1)
        {
            // do some thing
        }
        else
        {
            // do some thing
        }
    }
    

    PHP:

    if( check here file uploads or not )
    {
        echo 1;
    }
    else
    {
        echo 0;
    }
    
    点赞 评论 复制链接分享

相关推荐