2014-01-30 97 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