我想在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;
}
嗯..在我的AJAX成功,有一個成功插入數據的警報。如果文件沒有上傳,數據不會被插入,但ajax正在工作。我如何將'echo $ this-> upload-> display_errors()'發送給我的ajaxx成功/錯誤。所以,如果數據沒有上傳,就會有一條消息說明爲什麼沒有上傳。太大或錯誤的擴展名。對不起,長評論:D – Shota
要顯示錯誤,您需要使用$ this-> upload-> display_errors()從服務器端發送錯誤,然後在jquery中,如果成功標誌爲0,您將在json響應中獲得upl_error字符串,您可以使用它顯示錯誤。 檢查我更新的答案。 –
謝謝。我會試一試 – Shota