我正在尝试将ajax数据发送回视图,但它无法正常工作
这是我的控制器:
$this->autoRender = 0;
$this->layout = 'ajax';
$data = $this->HostingAccount->HostPackage->find('list',array('conditions' => array(
'HostPackage.host_id' => $this->data['HostingAccount']['host_id'])));
//can't get access to $options in my View
set('options',$data);
在default.ctp中我有:
echo $this->Js->writeBuffer(array('inline' => true));
在ajax.ctp中:
<?php echo $this->fetch('content'); ?>
我尝试过使用不同的设置,例如评论$ this-&gt; autoRender,只是尝试从默认视图而不是ajax访问$ options,它只是不让我从中获取数据。
请帮助,谢谢!
答案 0 :(得分:0)
好的,所以我错过了处理AJAX请求的函数的View,只需在控制器中为函数ajax_populate()创建一个View ajax_populate.ctp我能够从该视图中检索数据并使用它进一步说:
控制器:
public function ajax_populate() {
$this->layout = 'ajax';
$data = $this->Domain->HostingAccount->HostPackage->find('list',array('conditions' => array(
'HostPackage.host_id' => $this->data['Domain']['host_id'])));
$this->set('options', $data);
}
查看:
<?php
echo "<div><select>";
foreach($options as $key => $val){
echo "<option value='$key'>$val</option>";
}
echo "</select></div>";
原始视图(从中调用ajax):
echo $this->Js->get('#DomainHostId')->event('change',$this->Js->request('ajax_populate',
array('method' => 'POST',
'async' => true,
'update' => '#host_packages',
'dataExpression' => true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
//'evalScripts' => true
)));
&GT;
HTML:(div)
<div id="host_packages"></div>