dropzone.js 成功后重定向无法从 php codeigniter 返回的 json 工作
dropzone.js redirect after success not working from json returned by php codeigniter
我尝试在我的 dropzone 上传成功后使用 $.getJSON 从我的控制器回显 json_encode 进行重定向,但似乎我的 javascript 实际上没有得到 json_encode 因为我尝试在 $.getJSON
中添加警报
这是我的空降区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
@ourmandave 给出了正确的答案,我只需要更改这行代码
success: function(file, response){
$.getJSON("<?php echo(base_url('upload/image')); ?>", function(data){
window.location = data.urllink;
});
}
删除 $.getJSON 并使用 response 变量,因为成功函数会自动获取 json 响应,所以它看起来像这样
success: function(file, response){
window.location = response.urllink;
}
我尝试在我的 dropzone 上传成功后使用 $.getJSON 从我的控制器回显 json_encode 进行重定向,但似乎我的 javascript 实际上没有得到 json_encode 因为我尝试在 $.getJSON
中添加警报这是我的空降区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
@ourmandave 给出了正确的答案,我只需要更改这行代码
success: function(file, response){
$.getJSON("<?php echo(base_url('upload/image')); ?>", function(data){
window.location = data.urllink;
});
}
删除 $.getJSON 并使用 response 变量,因为成功函数会自动获取 json 响应,所以它看起来像这样
success: function(file, response){
window.location = response.urllink;
}