【发布时间】:2018-02-11 02:34:24
【问题描述】:
我尝试在我的 dropzone 上传成功后使用来自控制器的 echo json_encode 中的 $.getJSON 进行重定向,但似乎我的 javascript 实际上并没有获取 json_encode,因为我尝试在 $.getJSON 中添加警报
这是我的 dropzone javascript
Dropzone.options.myAwesomeDropzone = {
url: "<?php echo(base_url('upload/image')); ?>",
addRemoveLinks: true,
autoProcessQueue: false,
autoDiscover: false,
maxFilesize: 5,
maxFiles: 1,
acceptedFiles: ".png,.jpg,.gif",
previewsContainer: '#dropzonePreview',
clickable: "#dropzonePreview",
accept: function(file, done) {
console.log("uploaded");
done();
},
init: function() {
var myDropzone = this;
//now we will submit the form when the button is clicked
this.on("addedfile", function(file) {
$('.dz-message').hide();
$('#dropzonenoimageerrormessage').hide();
});
this.on("reset", function(file) {
$('.dz-message').show();
});
this.on("maxfilesexceeded", function(file) {
this.removeAllFiles();
this.addFile(file);
});
$("#sbmtbtn").on('click',function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
if ($('#my-awesome-dropzone').valid() == true) {
if (myDropzone.files.length) {
myDropzone.processQueue();
// upload files and submit the form
} else {
$('#dropzonenoimageerrormessage').show();
}
}
});
}, // init end
success: function(file, response){
$.getJSON("<?php echo(base_url('upload/image')); ?>", function(data){
window.location = data.urllink;
});
}
};
这是我的控制器中尝试发送 json 的片段
if(move_uploaded_file($tempFile, $targetFile)){
$newposturl = base_url('post/image/'.$data['recentlyuploadedimage']['slug']);
$arr = array('urllink' => "$newposturl");
echo json_encode($arr);
}
文件确实被移动了,数据被存储在数据库中,但我的 javascript 似乎无法获取 json
【问题讨论】:
-
success:函数应该在response变量中返回来自 CI 的 json。你不需要使用$.getJSON。 See this answer for example. -
这是否意味着我应该删除 remove
$.getJSON并只使用windows.location = response.urllink;? -
@ourmandave 哦,它成功了,谢谢伙计!我如何将您的评论作为答案?
标签: php json codeigniter dropzone.js