【问题标题】:In cakephp Jquery ajax get not working in chrome.在 cakephp Jquery ajax 无法在 chrome 中工作。
【发布时间】:2015-03-03 17:15:32
【问题描述】:

我使用的是 cakephp 2.4,这里我使用下面的代码在 searchdue 方法中将搜索值发送到 ReportsContrller。

           $('.search').keyup(function(){
                var search=$('.search').val();
                console.log(search);
                $.get("<?php echo Router::url(array('controller'=>'Reports','action'=>'searchdue'));?>",
                       {'search':search},
                       function(data){
                          $('.backsearch').html(data);
                       }
                      )
           })

然后在searchdue方法中我写了下面的代码

public function searchdue()
    {
        if(isset($this->request->query['search'])){
            $search = $this->request->query['search'];

        }
        else{
                $search = '';
            }
        $this->Paginator->settings = array(
            'conditions' => array('Report.id LIKE' => "$search"),
            'limit'=>1
        );
        $this->set('reports',$this->Paginator->paginate());
    }

如果没有 firefox,此代码不支持其他浏览器。此处 Keyup 事件工作正常,但数据未发送到方法。

【问题讨论】:

  • 您应该在 searchdue() 中回显数据,因为您从 cakephp 获得 AJAX 结果作为回报!!!
  • 视图模板正在生成但没有数据。
  • 使用$this-&gt;request-&gt;params['search'] 而不是你的$this->在第一、二行的请求。
  • 在你的函数中,使用 echo "
    ";print_r($search);你得到什么 ajax 结果?
  • 这里我没有得到任何结果,这里搜索值没有发送。

标签: jquery cakephp cakephp-2.4


【解决方案1】:

在您的控制器操作中,设置变量以查看后,您可以添加以下行:

public function searchdue() {
    // (...)
    $this->set('reports',$this->Paginator->paginate());

    $this->layout = null;
    $this->autoRender = false;
    $content = $this->render();
    die($content->body());
}

为确保正确返回所有内容,您还可以在通过die(); 完成操作之前使用json_encode();

所以最后两行可以改成:

// (...)
$content = $this->render()->body();
$result = array('content' => $content);
die(json_encode($result));

那么你还必须稍微改变你的 ajax 调用:

$.get("<?php echo Router::url(array('controller'=>'Reports','action'=>'searchdue'));?>", {'search':search}, function(data) {
   $('.backsearch').html(data.content);
}, 'json');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2013-05-21
    • 1970-01-01
    • 2015-04-04
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多