duanfei8897 2017-02-09 06:34
浏览 73
已采纳

如何在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 2017-02-09 06:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?