我是cakephp的新手并尝试实施AJAX
。我有一个视图add.ctp
,其中我写了以下几行:
$('#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);
}
});
}
});
get_office_names_by_catagory()
中的OfficenamesController.php
函数是:
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 :(得分:3)
可能由两个问题引起:
1)在你的js片段中,你正在查询
http://localhost/testpage/officenames/get_office_names_by_catagory/
。
请注意get_office_names_by_category
中的复数“姓名”。在PHP代码段中,您已定义了一个操作get_office_name_by_catagory
。请注意单数“名称”。
2)您可能需要适当设置标题,以便整个页面不会在AJAX请求中呈现:请参阅此link。
答案 1 :(得分: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);
}
});
您应该以key:value格式指定数据。
答案 2 :(得分: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的请求,因为函数中已经定义了一个参数$ $,所以如果我有/ get_office_name_by_catagory / 2的请求那么你会在$中找到值在行动中输入。
所以不需要使用$ _GET并且休息一切都很好!
答案 3 :(得分:0)
试试这个, 从ajax中删除类型并尝试。
$('#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 !';
}