【问题标题】:Simple ajax function in cakephp 2.x not workingcakephp 2.x 中的简单 ajax 函数不起作用
【发布时间】:2014-01-30 05:41:43
【问题描述】:

我是 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 !';    
}

但不幸的是,它没有提醒任何东西!怎么了 ?

【问题讨论】:

  • 不要在函数中只使用echo "Hello !"
  • @Rikesh 还是一样..没有警报
  • 是否在 firebug 上显示 ajax 成功请求?
  • Get_office_name 或 Get_office_names ?

标签: php jquery ajax cakephp


【解决方案1】:

可能是由两个问题引起的:

1) 在你的 js sn-p 中,你正在查询

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

注意get_office_names_by_category 中的复数“名称”。在 PHP sn-p 中,您已经定义了一个动作 get_office_name_by_catagory。请注意单数“名称”。

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

【讨论】:

    【解决方案2】:

    我认为,您指定的数据格式错误:

    $.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 格式指定数据。

    【讨论】:

      【解决方案3】:
      $('#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,因为函数中已经定义了一个参数 $type,所以如果我有 /get_office_name_by_catagory/2 的请求,那么你会在 $ 中找到值输入动作。

      所以不需要使用 $_GET 并且一切都很好!

      【讨论】:

        【解决方案4】:

        试试这个, 从 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 !'; 
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-12
          • 1970-01-01
          • 2018-09-13
          • 1970-01-01
          • 1970-01-01
          • 2014-09-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多