【发布时间】:2011-11-07 04:30:51
【问题描述】:
当用户上传照片时,它会通过控制器 (profile/upload_picture) 上传,该控制器运行文件类型、文件大小等验证。
如果验证返回错误,该错误会通过前端的 Ajax 显示。这行得通。
但如果验证没有返回错误,我想自动加载另一个允许用户裁剪图片的控制器/视图(profile/crop_picture)。
这是否可以通过 Jquery 使用 malsup's Form plugin?
下面的代码在成功后不起作用,我仍然停留在上传页面。
这是我的代码:
jQuery 视图
$("#file_select").change(function(){
var options = {
type: 'post',
dataType: 'json',
resetForm: true,
beforeSubmit: function(){
$('#loading').show();
$('#validation_message').html('');
},
success: function(data){
if (data.success == 0) {
$('#loading').hide();
$('#validation_message').html(data.message).fadeIn();
} else {
$('body').load('profile/crop_picture');
}
}
};
$('#upload_form').ajaxSubmit(options);
});
上传图片控制器
function upload_picture()
{
if ($this->input->post('upload')) {
$upload = $this->profile_model->upload_picture();
if ($upload) { // if there are upload errors
$val['success'] = '0';
$val['message'] = '<div class="error_message" style="margin:20px 0">' . $upload . '</div>';
echo '<textarea>' . json_encode($val) . '</textarea>';
} else {
//redirecting here seems to have no effect
}
}
$data['selector'] = 'picture';
$data['records'] = $this->profile_model->get_user_data();
$string = $this->load->view('account/prf/profile_crop_vw', $data, TRUE);
$this->template->write('content', $string);
$this->template->render();
}
【问题讨论】:
-
您能否确认您的成功回调是否运行?听起来它在这两种情况下都运行,因为您说过在出现验证错误时可以看到错误消息。所以真的不归结为“$('body').load('profile/crop_picture');”正在工作中?您可以在该行之前发出警报以确认它运行吗?然后,您还可以添加一个完成的回调来查看您的crop_picture ajax 方法是否有效: $('body').load('profile/crop_picture', function(txt, txtStatus) { alert('Load was executed: ' + txt状态);}
标签: php javascript jquery ajax codeigniter