2014-01-30 61 views
0

我是cakephp的新手,试图实现AJAX。我有我写了下面的线图add.ctpcakephp 2.x中的简单ajax函数不起作用

$('#office_type').change(function(){ 
    var office_id = $('#office_type').val(); 
    if(office_id > 0) { 
    var data = office_id; 
    var url_to_call = "http://localhost/testpage/officenames/get_office_names_by_catagory/"; 
    $.ajax({ 
     type: "GET", 
     url: url_to_call, 
     data = data, 
     //dataType: "json", 
     success: function(msg){ 
      alert(msg); 
     } 
    }); 
    } 
}); 

并在OfficenamesController.php功能get_office_names_by_catagory()是:

public function get_office_name_by_catagory($type = '') { 
    Configure::write("debug",0); 
    if(isset($_GET['type']) && trim($_GET['type']) != ''){ 
     $type = $_GET['type']; 
     $conditions = array("Officename.office_type"=> $type); 
     $recursive = -1; 
     $office_names = $this->Officename->find("all",array("conditions"=>$conditions,"recursive"=>$recursive)); 
    } 
    $this->layout = 'ajax'; 
    //return json_encode($office_names); 
    return 'Hello !';  
} 

但不幸的是,它没有任何警告!怎么了 ?

+0

不要在函数中使用只返回'echo“Hello!”''。 – Rikesh

+0

@Rikesh仍然是一样..没有提醒 – Nitish

+0

它显示ajax成功请求萤火虫? –

回答

0

我想,你在错误的格式指定数据:

$.ajax({ 
    type: "GET", 
    url: url_to_call, 
    data = data,    // i guess, here is the problem 
    //dataType: "json", 
    success: function(msg){ 
     alert(msg); 
    } 
}); 

$.ajax({ 
    type: "GET", 
    url: url_to_call, 
    data: { name: "John", location: "Boston" }, //example 
    success: function(msg){ 
     alert(msg); 
    } 
}); 

就应该在关键的数据:值格式。

0
$('#office_type').change(function(){ 
    var office_id = $('#office_type').val(); 
    if(office_id > 0) { 
    var data = office_id; 
    var url_to_call = "http://localhost/testpage/officenames/get_office_name_by_catagory/"+office_id; 
    $.ajax({ 
     type: "GET", 
     url: url_to_call, 
     success: function(msg){ 
      alert(msg); 
     } 
    }); 
    } 
}); 

在你的行动

public function get_office_name_by_catagory($type = '') { 
    $this->autoRender = false; 
    Configure::write("debug",0); 
    if(!empty($type)){ 

     $conditions = array("Officename.office_type"=> $type); 
     $recursive = -1; 
     $office_names = $this->Officename->find("all",array("conditions"=>$conditions,"recursive"=>$recursive)); 
    } 
    $this->layout = 'ajax'; 
    //return json_encode($office_names); 
    echo 'Hello !'; 
    exit; 
} 

看看我做的是我已经改变了你的要求的功能get_office_name_by_catagory,因为有一个paramenter $类型是在函数已经被定义,所以如果我有请求/ get_office_name_by_catagory/2,然后您将在$ type中找到值。

所以没有必要使用$ _GET并休息一切都很好!

0

试试这个, 从ajax中删除类型并尝试。

1)在你的js代码段,您要查询

http://localhost/testpage/officenames/get_office_names_by_catagory/

$('#office_type').change(function(){ 
    var office_id = $('#office_type').val(); 
    if(office_id > 0) { 
    var data = office_id; 
    var url_to_call = yourlink +office_id; 
    **$.ajax({ 
     url: url_to_call, 
     success: function(msg){ 
      alert(msg); 
     } 
    });** 
    } 
}); 

在你的行动

public function get_office_name_by_catagory($type = '') { 
    $this->autoRender = false; 
    Configure::write("debug",0); 
    if(!empty($type)){ 

     $conditions = array("Officename.office_type"=> $type); 
     $recursive = -1; 
     $office_names = $this->Officename->find("all",array("conditions"=>$conditions,"recursive"=>$recursive)); 
    } 
    $this->layout = 'ajax'; 
    //return json_encode($office_names); 
    echo 'Hello !'; 
} 
3

可以通过两个问题引起的。

请注意get_office_names_by_category中的复数“名称”。在PHP代码片段中,您定义了一个动作get_office_name_by_catagory。请注意单数的“名称”。

2)您可能需要适当地设置您的标题,以便整个页面不会在AJAX请求上呈现:请参阅此link