【问题标题】:Cakephp 3 & Ajax, send $.get additional dataCakephp 3 & Ajax,发送 $.get 附加数据
【发布时间】:2014-12-23 02:37:00
【问题描述】:

我尝试使用来自 jquery 的 $.get 发送自定义数据,但 cakephp 3 无法识别变量。

这是我在控制器中的功能:

public function view($cat,$id,$page){
        $comments = $this->Comments->find('all')->where(['category =' => $cat, 'AND' => ['category_id =' => $id]])->order(['comments.created' => 'DESC'])
                        ->contain([
                            'Reports' => function($q){return $q->where(['user_id =' => $this->Auth->user('id')]);},
                            'Chars' => function($q){ return $q->select(['id','name','class','race','level','guild_id', 'user_id'])
                            ->contain(['Guilds' => function($q){ return $q->select(['id','name']);
                            }]);
                        }])->limit(3)->page($page);
if($cat == 'Videos'){
                $subject = $this->Comments->Videos->get($id);
            }
            $category = $cat;
            $this->set(compact('subject','comments','category'));
    }
}

这是.js

$('.more').click(function(){

    $.get($(this).attr('href'),{cat:'Videos',id:'44',page:'2',function(data){
        $('.coms').empty().append(data);
    });
    return false;

});

还有链接:

<?= $this->Html->link('More', ['controller' => 'Comments','action' => 'view'], ['class' => 'btn btn-primary more']) ?>

.js 中的修复值用于测试,如果我使用 $.get(href) 在链接中发送数据,它可以工作,但我想知道如何在 $.get 请求中传递自定义数据。

感谢您的帮助

【问题讨论】:

    标签: json ajax cakephp-3.0


    【解决方案1】:

    CakePHP 不会神奇地将查询字符串参数映射到方法参数,路由甚至都不支持。

    您可以通过请求对象访问查询字符串参数

    $this->request->query('cat');
    

    另请参阅http://book.cakephp.org/3.0/en/controllers/request-response.html#query-string-parameters

    【讨论】:

      【解决方案2】:

      解决方案

      嗨,我做了一个小 api 从 json 数组中获取我需要的数据。

      所以我创建了一个 ApiController 来处理我需要的所有 ajax 请求。这是测试示例:

      <?php
      namespace App\Controller;
      
      use App\Controller\AppController;
      use Cake\ORM\TableRegistry;
      
      class ApiController extends AppController
      {
          public function test($id)
          {
              $UsersTable = TableRegistry::get('Users');
              $users = $UsersTable->find()->where(['id <' => $id]);
              $this->set(compact('users'));
          }
      }
      ?>
      

      我想从 ajax 调用中获取我所有用户的列表(这是有史以来最糟糕的想法,但它只是为了测试;))

      现在我需要为此视图启用 json。为此,我需要进入 config/routes.php 来添加:

      Router::scope('/api', function($routes){
        $routes->extensions(['json']);
        $routes->connect('/test/*', ['controller' => 'Api', 'action' => 'test', '_ext' => 'json']);
      });
      

      现在当我继续 http://localhost/mywebsite/api/test/100 时,我有一个 json 数组,其中包含所有 id

      '_ext' => 'json'
      

      意味着您不必继续使用 api/test.json 来显示 json 数组

      现在在随机控制器中的随机视图上,我放了一个按钮来尝试所有这些,然后我调用我的 javascript 文件来执行我的 ajax 请求。

      <button class="test">Test button</button>
      <?= $this->Html->script('test', ['block' => true]) ?>
      

      现在在我的 test.js 中:

      $(document).ready(function(){
        var value = 100;
        $('.test').click(function(){
          $.get('/api/test/' + value, function(data){
              console.log(data);
          });
        });
      });
      

      我单击按钮,当我按下 F12(在 Chrome 上)时,日志会在我的控制台中显示一个包含我想要的所有用户的数组。

      希望这会对某人有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-14
        • 1970-01-01
        • 1970-01-01
        • 2013-07-04
        • 1970-01-01
        • 2016-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多