2017-02-09 41 views
1

我想在ajax警報錯誤消息上顯示文件上傳錯誤。如何在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; 
     } 

回答

0

您需要發送一個標誌來指定,如果文件成功與否上傳至您只需發送一個成功標誌從你的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)); 
} 

然後在你的阿賈克斯成功回調函數,你需要檢查這樣的成功標誌

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

     } 
}); 

我希望它能幫助你。

+0

嗯..在我的AJAX成功,有一個成功插入數據的警報。如果文件沒有上傳,數據不會被插入,但ajax正在工作。我如何將'echo $ this-> upload-> display_errors()'發送給我的ajaxx成功/錯誤。所以,如果數據沒有上傳,就會有一條消息說明爲什麼沒有上傳。太大或錯誤的擴展名。對不起,長評論:D – Shota

+0

要顯示錯誤,您需要使用$ this-> upload-> display_errors()從服務器端發送錯誤,然後在jquery中,如果成功標誌爲0,您將在json響應中獲得upl_error字符串,您可以使用它顯示錯誤。 檢查我更新的答案。 –

+0

謝謝。我會試一試 – Shota

0

的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; 
} 
+0

那麼,它是不可能的? – Shota

+0

沒有它的可能,在成功阻止顯示錯誤取決於PHP腳本返回的響應 –

+0

如何將file_upload錯誤文本返回到ajax成功塊? – Shota

0

在成功函數來檢查響應的內容,或者使用http_response_code(500)在php腳本中觸發ajax錯誤函數。