【问题标题】:How to display file upload error message on ajax error?如何在ajax错误上显示文件上传错误消息?
【发布时间】:2017-06-27 01:20:08
【问题描述】:

我想在 ajax 警报错误消息中显示文件上传错误。

这是我的 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

        }
    });

AJAX 错误警报仅在 AJAX 不工作时显示警报。我想在这里显示文件上传错误。

这是我在不上传时处理文件上传的控制器:

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

        $response_array = array ('upl_error' => $this->upload->display_errors());
        echo json_encode($response_array);
        //return false;
        }

【问题讨论】:

    标签: javascript php jquery ajax file-upload


    【解决方案1】:

    您需要发送一个标志来指定文件是否成功上传,这样可以从您的 php 代码中发送一个成功标志,如下所示:

    PHP 代码

    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));
    }
    

    然后在您的 ajax 成功回调函数中,您需要像这样检查成功标志

    jQuery 代码

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

    希望对你有帮助。

    【讨论】:

    • 嗯..在我的AJAX成功中,有一个成功插入数据的警报。如果文件未上传,则数据未插入,但 ajax 正在工作。如何将 echo $this-&gt;upload-&gt;display_errors() 发送到我的 ajaxx 成功/错误。因此,如果数据未上传,则会显示为什么未上传数据的消息。扩展名太大或错误。对不起,长评论:D
    • 要显示错误,您需要使用 $this->upload->display_errors() 从服务器端发送错误,然后在 jquery 中,如果成功标志为 0,您将在 json 响应中获得 upl_error 字符串,您可以用于显示错误。检查我更新的答案。
    • 它显示错误SyntaxError:JSON.parse:unexpected character at line 1 column 1 of the JSON data。错误应该是The file is too big,因为我上传的文件超出了允许的大小
    • 在您的成功回调中添加 console.log(data) 并检查您从服务器获得的响应。它应该像这样 {"success"=0,"upl_error":"your error message"} (如果发生错误)
    • 我把它放在 AJAX 错误中,它可以工作。 error: function (jqXHR, textStatus, errorThrown, data) { toastr.options = { closeButton : false, progressBar : false, showMethod : 'slideDown', timeOut : 3000 }; toastr.error(errorThrown, 'Error...'); if(data.upl_error) { toastr.error(data.upl_error, 'Error...'); }
    【解决方案2】:

    AJAX 错误警报仅在 AJAX 不工作时显示警报。这工作正常,因为您的文件未上传的情况不是 ajax 错误,而是正常的代码流。

    要处理它,您必须在 ajax 的success 中返回一些状态来管理它,例如:

    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;
    }
    

    【讨论】:

    • 那么,这不可能吗?
    • 不可能,成功块显示错误取决于php脚本返回的响应
    • 如何将 file_upload 错误文本返回到 ajax 成功块?
    • 我想返回$this-&gt;upload-&gt;display_errors()的文本。所以用户知道为什么数据没有上传
    【解决方案3】:

    在成功函数中检查响应内容,或者在php脚本中使用http_response_code(500)触发ajax错误函数。

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多