ajaxSubmit的回调函数没有触发

3

from File upload with jQuery and CodeIgniter (But no page refresh)

@cbrandolino suggest me to use jQuery form plugin to solve my problem. It was working well except for one issue, the callbackfunction isn't fire as I expected.

Here's my modified code
++ Javascript ++

$('#send_button').live('click', function(){
    var options = {
        url: 'formHandle/add',
        success: function(){
            alert('something');
        }
        //I tried both function() or function(data) 
        //or a function created outside $(document).ready()
        //None of them works
    };
    $('#new_entry_form').ajaxSubmit(options);
})

++ Controller ++

function add(){
    $array = array(
        'title' => $this->input->post('title'),
        'description' => $this->input->post('description'),
        'content' => $this->input->post('content'),
        'start' => $this->input->post('start'),
        'due' => $this->input->post('due'),
        'price' => $this->input->post('price'),
        'promotion_price' => $this->input->post('promotion_price'),
        'website' => $this->input->post('website'),
        'author' => $this->input->post('author'),
    );

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['overwrite'] = false;
    $config['remove_spaces'] = true;
    $config['encrypt_name'] = true;

    $this->load->library('upload', $config);
    if($this->upload->do_upload('picture')){
        $array['picture'] = $this->upload->file_name;
        $this->load->model('entry_model');
        $this->entry_model->insertEntry($array);
        echo 'true'; 
        //I tried $this->load->view, echo or return
        //none of them works
    }else{
        echo 'false';
    }
}

The file upload was working, the file was uploaded to the specific folder.
The data insertion was working, the data was inserted to the database as expected.
The only problem I have is the callback function.

Do you have any idea how to fix this? I need to show the result of data insertion.

Update:
Adding return false help me nothing. Still not working


如果您的帖子(如果是帖子)在提交时重新加载页面,那么在您的ajax调用之后返回false;将解决您的问题。 - Johan Olsson
嘿,我也遇到了同样的问题,你是怎么解决的? - NestedWeb
2个回答

2

在选项中编写回调函数,明确地写在这里:此处

$(document).ready(function() { 
var options = { 
    target:        '#output2',   

    success:       showResponse  // post-submit callback 
 }; 

// bind to the form's submit event 
$('#myForm2').submit(function() { 
    // inside event callbacks 'this' is the DOM element so we first 
    // wrap it in a jQuery object and then invoke ajaxSubmit 
    $(this).ajaxSubmit(options); 

    // !!! Important !!! 
    // always return false to prevent standard browser submit and page navigation 
    return false; 
}); 
}); 

function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 

    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 

    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 

    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
}

谢谢您的回复。但是,根据我的代码编写方式,我从未提交过表单。我通常使用serialize()方法检索到的表单数据发送到控制器。这是我第一次使用ajaxSubmit提交表单。因此,我的表单或提交按钮没有任何操作。我使用一个div作为按钮。所以,我不需要“return false;”。我认为我的代码是有效的,因为数据已插入,图片已上传。我只是不知道为什么回调函数没有按预期工作。我不知道是否有我不知道的东西。 - Tar_Tw45

1

你的表单有 onsubmit="return false;" ??


我从不提交我的表单。通常我会使用serialize()方法检索到的表单数据发送给控制器。这是我第一次提交表单,但是在ajaxSubmit的帮助下完成了。因此,我的表单或提交按钮没有任何操作。我使用一个div作为按钮,并向该div按钮添加代码(在这种情况下,该div是“send_button”)。因此,我不需要“return false;”,因为根本不会提交任何表单。 - Tar_Tw45

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接