[英]Returning Uploadifive Ajax 'Error/Success' data from Codeigniter Controller
I'm using the Uploadifive upload plug-in.我正在使用Uploadive上传插件。 It works as it should.
它可以正常工作。 However, I'm having a hard time passing
error
and success
messages from my controller back to my view via the plug-in.但是,我很难通过插件将
error
和success
消息从控制器传递回我的视图。 I'm logging console and log
errors.我正在记录控制台并
log
错误。 But can't seem to return any controller errors back to the Uploadifive call to display in my view.但似乎无法将任何控制器错误返回给 Uploadive 调用以显示在我的视图中。
In short, I want to output either an error
or success
message from my controller (via the $result
var) back to my ajax function to embed into my view.简而言之,我想将
error
或success
消息从我的控制器(通过$result
var)输出回我的 ajax 函数以嵌入到我的视图中。
The alert, alert(data.result)
outputs "undefined".警报
alert(data.result)
输出“未定义”。
JS function: JS函数:
$('#file-upload').uploadifive({
...
'onUpload': function(file) {
console.log('upload complete!');
},
'onUploadComplete' : function(file, data) {
console.log('The file ' + file.name + ' uploaded successfully.');
// returned error/success message here
alert(data.result);
},
'onError': function(errorType) {
console.log(errorType);
}
});
CI Controller method: CI控制器方法:
function add_document() {
// If Ajax request, proceed:
if(isset($_POST['is_ajax'])) {
if (!$this->upload->do_upload()) {
// If file upload failed or is invalid,
// display error notification
$result = array('error' => $this->upload->display_errors());
echo json_encode($result);
}
else {
// If file upload was successful
$result = 'success!';
echo $result;
...
}
}
// if not, redirect to upload page
else {
redirect('upload');
}
}
I figured out how to get this to work with my controller code.我想出了如何让它与我的控制器代码一起工作。 Uploadifive is not well documented, so it's been a shaky ride.
Uploadifive 没有很好的文档记录,所以这是一个不稳定的旅程。 But none the less, here's how I got it to function the way I wanted.
但无论如何,这就是我如何让它按照我想要的方式运行。 Hopefully this helps others.
希望这对其他人有帮助。
In my controller, I replaced the echo json_encode($result);
在我的控制器中,我替换了
echo json_encode($result);
line with the below.与下面的一致。 Specifying the array key I want to use (ie error) and the outputting of the result as
html
.指定我想使用的数组键(即错误)并将结果输出为
html
。
echo html_entity_decode($result['error']);
In my javascript function, all I simply needed to do was output the data
... .result
was not needed in the alert.在我的 javascript 函数中,我只需要输出
data
......警报中不需要.result
。 Note, I decided to attach the result to some html
versus an alert.请注意,我决定将结果附加到一些
html
而不是警报。
// Display Error/Success Messages
$('.status').html(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.