2016-10-26 43 views
0

我已经做了功能,我可以在确认后添加一行。问题是,提交按钮后,表不会重新加载并显示错误功能alert.actually数据成功保存,我必须刷新页面,以便表可以重新加载。这里是我的ajax jQuery代码:函数保存ajax错误

function reloadPage() 
 
    { 
 
    window.location.reload() 
 
    } 
 

 
function save() 
 
{ 
 
    $('#btnSave').text('saving...'); //change button text 
 
    $('#btnSave').attr('disabled',true); //set button disable 
 
    var url; 
 
    
 
    if(save_method == 'add') { 
 
     url = "<?php echo site_url('activity/save')?>"; 
 
    } else { 
 
     url = "<?php echo site_url('activity/update_activity')?>"; 
 
    } 
 

 
    
 
    // ajax adding data to database 
 
    $.ajax({ 
 
     url : url, 
 
     type: "POST", 
 
     data: $('#form-input').serialize(), 
 
     dataType: "JSON", 
 
     success: function(data) 
 
     { 
 
    
 
      
 
      $('#myModal').modal('hide'); 
 
      reloadPage(); 
 
$('#btnSave').text('save'); //change button text 
 
     $('#btnSave').attr('disabled',false); //set button enable 
 

 

 
    }, 
 
    error: function (jqXHR, textStatus, errorThrown) 
 
    { 
 
     alert('Error adding/update data'); 
 
     $('#btnSave').text('save'); //change button text 
 
     $('#btnSave').attr('disabled',false); //set button enable 
 

 
    } 
 
}); 
 
}
<button id="btnSave" onclick="save()" class="btn green"> 
 
"fa fa-save"> save</button>

我的控制器:

public function save() { 
 
     $actype  \t = $this->input->post('actype'); 
 
     $activity_name \t = $this->input->post('activity_name'); 
 
     $project \t = $this->input->post('project'); 
 
     $portion \t = $this->input->post('portion'); 
 
     $activity \t = $this->input->post('actid'); 
 
    
 

 
     $data = array(
 
     \t 'activity_type_id'=>$actype, 
 
\t \t 'activity_name' \t =>$activity_name, 
 
\t \t 'project_id' \t =>$project, 
 
\t \t 'portion' \t =>$portion, 
 
\t \t 'activity_id' \t => $activity 
 
      
 
     ); 
 
     $this->activity->insertactivity($data); 
 
    
 
     redirect("activity/input"); 
 
    }

我点击保存按钮后,警报('错误添加/更新数据'),但实际上在重新加载页面数据保存后。 哪里是我的ajax代码中的代码错误?

+0

你有两个功能节省???在控制器和脚本? –

+0

是的,它是不是? –

+0

我认为它错了。为什么你需要两个功能相同的功能(保存)? –

回答

0

强制从服务器重新加载。

window.location.reload(true); 

如果不指定true重装可能会从浏览器缓存(如果可用)。

此外,在控制器中,redirect("activity/input");不是对AJAX请求的适当响应。试试像这样的东西。

$this->activity->insertactivity($data); 
echo json_encode(array('result' => TRUE)); 

您的控制器代码也可以更加简洁。考虑这个

public function save() 
{ 
    $data = array(
      'activity_type_id' => $this->input->post('actype'), 
      'activity_name' => $this->input->post('activity_name'), 
      'project_id' => $this->input->post('project'), 
      'portion' => $this->input->post('portion'), 
      'activity_id' => $this->input->post('actid') 
    ); 

    //Assuming insertactivity returns TRUE if the insert works and FALSE if not 
    $results['result'] = $this->activity->insertactivity($data); 
    echo json_encode($results); 
} 

您可以在成功的功能检查“结果”

success: function(data) 
{ 
    if(data.result === true) 
    { 
    $('#myModal').modal('hide'); 
    reloadPage(); 
    $('#btnSave').text('save'); //change button text 
    $('#btnSave').attr('disabled',false); //set button enable 
    } else { 
    //do something to the DOM to tell about the problem 
    //which probably means you should add that to your controller's response. 
    } 
},