如何从codeigniter获取值到ajax?

时间:2013-11-14 09:50:27

标签: ajax codeigniter

我有一个ajax,我不知道它是否正确。我想从控制器获取值并将其传递给ajax。

AJAX:

$.ajax({
    type: "GET",
    url: swoosh(id, path+'swoosh_employee/swoosh_delete_child', 'childdv'),
    success: function(response) {
      if (response != "Error")
        { 
            $('#success-delete').modal('show');
        } 
        else 
        { 
        alert("Error"); 
        }
    }
  });
 event.preventDefault();

并在控制器中:

 public function swoosh_delete_child()
{
    $P1 = $this->session->userdata('id');
    parse_str($_SERVER['QUERY_STRING'],$_GET);
    $id = $_GET['h'];

    $response = $this->emp->delete_children($id);
}

模型

public function delete_chilren($id){
.......//codes here.. etc. etc.
if success // 
 return "success";
else
 return "Error";
}

我只想传递/获取$ reponse的值并将其传递给ajax并检查值是否为错误。

2 个答案:

答案 0 :(得分:2)

只需在控制器中回显:

$response = $this->emp->delete_children($id);

alert回复:

alert(response); //output: success / Error

答案 1 :(得分:1)

在您的控制器中:

你应该有这样的东西

public function swoosh_delete_child(){

$P1 = $this->session->userdata('id');
parse_str($_SERVER['QUERY_STRING'],$_GET);
$id = $_GET['h'];

$response['status'] = $this->emp->delete_children($id);
  echo json_encode($response);
}

然后在你的ajax中,访问响应

 $.ajax({
  type: 'POST',
  url: url: swoosh(id, path+'swoosh_employee/swoosh_delete_child', 'childdv'),,
  dataType: 'json',
  success: function(response){
  if (response.status)
    { 
        $('#success-delete').modal('show');
    } 
    else 
    { 
    alert("Error"); 
    }
  }
});