我想在 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 不工作时显示警报。我想在这里显示文件上传错误。
这是我的 Controller ,在不上传时处理文件上传:
if ( ! $this->upload->do_upload('file')){
$response_array = array ('upl_error' => $this->upload->display_errors());
echo json_encode($response_array);
//return false;
}
最佳答案
您需要发送一个标志来指定文件是否上传成功,要从您的 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);
}
}
});
希望对你有帮助。
关于javascript - 如何在ajax错误时显示文件上传错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129804/