Php Ajax图片上传刷新页面?

Php Ajax图片上传刷新页面?,php,jquery,ajax,Php,Jquery,Ajax,我想知道,为什么我用ajax上传图片的时候我的页面会刷新,我试着调试这个问题,但是没有发现, 这是控制器的功能 public function uploadImage() { if(isset($_FILES['header_image'])) { $config['upload_path'] = 'public/images/global/'; $config['allowed_types'] = 'gif|jpg|png'; $conf

我想知道,为什么我用ajax上传图片的时候我的页面会刷新,我试着调试这个问题,但是没有发现,

这是控制器的功能

public function uploadImage() {
    if(isset($_FILES['header_image'])) {
        $config['upload_path'] = 'public/images/global/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['file_name'] = 'b_'.random_string('alnum', 5);
        $this->upload->initialize($config);

        if($this->upload->do_upload('header_image')) {
            echo json_encode(array('status'=>"1", 'image_url'=>$this->upload->file_name));
        } else {
            $upload_error = array('error' => $this->upload->display_errors());
            echo json_encode(array('status'=>"0",'upload_error'=>strip_tags($upload_error['error'])));
        }
        exit;
    }
    $this->template->render();
}
虽然我的视图uploadImage.php看起来像

$(document).ready(function() {
$('#header_image').on('change', function(){
    $("#frm_global").ajaxForm({
        success: function(data) {
            res = $.parseJSON(data);
            if(res.status == 1){
                var html = "<img src='<?=base_url();?>path/"+res.image_url+"' class='preview' width='100' height='100' />";
                    html+= "<input type='hidden' id='image_url' name='image_url' value='"+res.image_url+"' >";
                    $("#preview").html(html);
                    $("#frm_global").ajaxFormUnbind();
            }
            if(res.status == 0){
                $("#frm_global").ajaxFormUnbind();
            }
        }
    }).submit();
});
});

<form name="frm_global" id="frm_global" enctype="multipart/form-data">
<div id="preview" style="float:left">
</div>
<div style="float:left">
    <div style="margin-top:25px">
        <input type="file" name="header_image" id="header_image"/>
        <input style="margin-left:100px" onclick="" type="image" id="upload_header" src="any path" />
    </div>
</div>
</form>
$(文档).ready(函数(){
$(“#标题_图像”)。在('change',function()上{
$(“frm#U global”).ajaxForm({
成功:功能(数据){
res=$.parseJSON(数据);
if(res.status==1){
var html=“path/”+res.image_url+“'class='preview'width='100'height='100'/>”;
html+=“”;
$(“#预览”).html(html);
$(“#frm#u global”).ajaxFormUnbind();
}
如果(res.status==0){
$(“#frm#u global”).ajaxFormUnbind();
}
}
}).submit();
});
});

对不起,我想我没有正确理解你的问题

但是你可以这样尝试提交

$("#theForm").ajaxForm({
url: 'server.php',
type: 'post',
success:function(data)
{
//do what you want to
}
});

您的图像是否正在上载?是的,图像正在上载。如果我删除结尾处的.submit(),请不要提交。您是否指定了url和类型?谢谢搜索,但此“ajaxForm”是该插件的一部分