[英]Simple ajax function in cakephp 2.x not working
I am new to cakephp and trying to implement AJAX
. 我是Cakephp的新手,正在尝试实现
AJAX
。 I have a view add.ctp
in which I have written the following lines : 我有一个视图
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);
}
});
}
});
And the function get_office_names_by_catagory()
within OfficenamesController.php
is: 而功能
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 !';
}
But unfortunately, its not alerting anything ! 但不幸的是,它没有提醒任何人! Whats wrong ?
怎么了 ?
Could be caused by two issues: 可能由两个问题引起:
1) In your js snippet, you are querying 1)在您的js代码段中,您正在查询
http://localhost/testpage/officenames/get_office_names_by_catagory/
. http://localhost/testpage/officenames/get_office_names_by_catagory/
。
Note the plural 'names' in get_office_names_by_category
. 请注意
get_office_names_by_category
的复数“名称”。 In the PHP snippet, you've defined an action get_office_name_by_catagory
. 在PHP代码段中,您定义了一个动作
get_office_name_by_catagory
。 Note the singular 'name'. 注意单数的“名称”。
2) You may need to set your headers appropriately so the full page doesn't render on an AJAX request: Refer to this link . 2)您可能需要适当设置标题,以使整个页面都不会在AJAX请求上呈现:请参阅此链接 。
I think, you have specified data in wrong format: 我认为您指定的数据格式错误:
$.ajax({
type: "GET",
url: url_to_call,
data = data, // i guess, here is the problem
//dataType: "json",
success: function(msg){
alert(msg);
}
});
To 至
$.ajax({
type: "GET",
url: url_to_call,
data: { name: "John", location: "Boston" }, //example
success: function(msg){
alert(msg);
}
});
You should specify the data in key:value format. 您应该以key:value格式指定数据。
$('#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);
}
});
}
});
In your action 在你的行动中
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;
}
See what I have done is I have changed your request to function get_office_name_by_catagory, as there is one paramenter $type is already defined in the function, so if I have the request by /get_office_name_by_catagory/2 then you will find value in $type in action. 看看我做了什么,因为我已经将您的请求更改为函数get_office_name_by_catagory,因为该函数中已经定义了一个参数$ type,所以如果我有/ get_office_name_by_catagory / 2的请求,那么您会在运行中的$ type中找到值。
So no need to use $_GET and rest everything is fine! 因此,无需使用$ _GET并确保一切正常!
Try this, remove type from ajax and try. 试试这个,从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);
}
});**
}
});
In your action 在你的行动中
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 !';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.