2014-10-07 158 views
1

我需要一些帮助,在我的问题。 我有一个用户列表,我想在CI HMVC中使用ajax删除用户onclick的删除按钮。这里是我的列表视图代码Codeigniter HMVC Ajax

$(function() { 
    $(".tip-del").click(function() { 
     var recId = $(this).data("id"); 
     var parent = $(this).parent(); 
     var BASE_URL = "<?php echo base_url()?>"; 
     var r = confirm("Are you sure you want to delete this user?"); 
     if(r == true){ 
      $.ajax({ 
       url : BASE_URL + 'auth/delete_user', 
       type: 'post', 
       data: {'rec_id' : recId }, 
       success: function (response){ 
        try{ 
         if(response == 'true'){ 
          parent.slideUp('slow',function(){$(this).remove();}); 
         } 
        }catch(e) {  
         alert('Exception while request..'); 
        }      
       }, 
       error: function (xhr, text, message) { 
        console.log(message); 
       } 
      }); 
      return false; 
     } 
    }); 
}); 

,这里是我的控制器(其中视图位于同一模块)代码

function delete_user() { 
    if ($this->input->post('rec_id')) { 
     $id = $this->input->post('id'); 
    } 
    if (!$this->ion_auth->in_group('admin')) { 
     $this->session->set_flashdata('message', $this->lang->line("access_denied")); 
     $data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message')); 
     redirect('module=auth&view=users', 'refresh'); 
    } 
    $this->ion_auth_model->deleteUser($id); 
} 

,这里是我的模型代码

public function deleteUser($id) { 
    if ($this->db->delete('users', array('id' => $id)) && $this->db->delete('users_groups', array('user_id' => $id))) { 
     return true; 
    } 
    return FALSE; 
} 

灿有人请帮助我。我不明白我出错了。在ajax响应的预览中,我得到的错误为

An Error Was Encountered. The action you have requested is not allowed. 

在此先感谢。

+0

是否存在与该错误关联的行号和文件,或者是从codeigniter返回的Web视图? – Wold 2014-10-07 05:39:25

+0

您检查以确保您的db用户具有执行delete()的适当权限,这可能是问题所在。 – Wold 2014-10-07 05:41:17

回答

0

请访问以下链接:

https://ellislab.com/forums/viewthread/163976/

您首先在谷歌搜索,如果你不知道答案,那么你就张贴在这里。

+0

感谢您的回复,对我有用,我将$ config ['csrf_protection']的 从TRUE更改为FALSE。它回应成功的回应,但不会从数据库中删除记录。请建议我出错了? – 2014-10-07 07:04:28

+0

使用$ this-> db-> last_query()来记录最后一个查询;并看看发生了什么。 – jewelnguyen8 2014-10-07 07:21:07

1

我认为你的删除查询是错误的。它应该像下面一样(在型号代码中):

if ($this->db->where(array('id' => $id))->delete('users') && $this->db->where(array('user_id' => $id))->delete('users_groups')) { 
     return true; 
} 
+0

谢谢Rahul,你解决了我的问题。非常感谢你。 – 2014-10-07 13:46:06

+0

你是最受欢迎的萨钦.... – 2014-10-08 05:34:44