【问题标题】:Avoid the display of default layout in AJAX request in CakePHP避免 CakePHP 中 AJAX 请求中显示默认布局
【发布时间】:2011-11-30 06:45:38
【问题描述】:

我想在触发 select 的事件 onchange 时显示一个测试 div,但结果显示 div 总默认布局。我只希望test.ctp 出现在我的结果 div 中。

这是我的代码,请告诉这里有什么问题。我有一个带有id="ProductCategoryId" 的下拉列表,结果是div,例如:

<div id="result">result html</div>

这是 jQuery 函数:

$("#ProductCategoryId").change(
  function() {
    var catid = $(this).val();
    $.post('product/test', {id: catid},
    function(result) {
      $('#result').html(result);
    });
  }
);

在我的product_controller

function test() {
  $this->autorender = false;
  $this->layout = 'ajax';
}

test.ctp 中只写入测试输出。

【问题讨论】:

  • 你能告诉我们test方法的完整代码吗?还是只是你放在这里的东西?
  • 是的,我要写代码,这只是开始,所以这是基本代码。在 test.ctp 中只是
    输出在这里
  • 您是否包含了RequestHandler 组件?
  • 是的,我已经包含了 RequestHandler 组件
  • 尝试使用浏览器开发工具(例如 Firebug)测试网络活动,以确认您发出的是 AJAX 请求而不是常规请求

标签: php javascript jquery cakephp cakephp-1.3


【解决方案1】:

不知道你有没有发现问题。好吧,如果不想在视图周围包裹布局,您可以简单地将布局设置为 false。而且我看到您已禁用自动渲染,这意味着您正在专门调用控制器的 render('test') 函数。??

   $this->autorender = false;
   $this->layout = false;
   $this->render('test');

【讨论】:

  • 问题解决了,就是链接:url应该是/myapplicationname/controller/function,function应该是function test(){ $id = $_POST['id']; $this->autoRender = false; $this->layout = "ajax"; $this->render('test'); } *引用这个:stackoverflow.com/questions/3623738/…
  • 对于其他找到这个的人,请注意它是:$this->autoRender = false; (带有大写字母 R)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
相关资源
最近更新 更多