我需要动态地将表单加载到页面中,并且可能存在此表单的多个实例。验证和保存不是问题,但我似乎无法从Controller传回实际的表单内容/标记。使用简单值(id:1)进行测试时,我可以成功返回数据,但如果未设置数据类型,则实际表单将始终返回为undefined,或者[Object object]。
控制器:
public function addPermissionFormAction()
{
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
//data from ajax call is successfully received
$username = $this->getRequest()->getParam('user');
//...Code to get $pages...
$form = new Pds_Wizard_SubForm_Externals($pages);
if(!isset($form)) $form = false;
//I have run XDebug to here and confirmed that the form is built successfully.
echo Zend_Json::encode(array('form' => $form));
}
我的Ajax电话:
$('#get-player-permissions').click(function() {
if($('#test-test').val().length > 0) {
$.ajax({
url: '/pds/external-user/add-permission-form',
data: { user: $('#test-test').val() },
dataType: 'json',
contentType: "application/json",
success: function(data){
alert(data.form); // [Object object]
$("#wizardContainer").html(data.form); //empty, no form content
}
})
}
});
我已经尝试了这一点,甚至没有添加 dataType 或 contentType 字段来发送简单的值,并且没有问题通过。但是,在这种情况下,我会得到一个null或undefined。
感谢任何建议,包括动态加载表单的更理想方法。我不经常使用Ajax调用,所以如果这是一个简单的错误我道歉。提前谢谢!
答案 0 :(得分:3)
在这种情况下,您无法设置“无渲染”。您需要有一个视图“add-permission-form.phtml”(或指定的其他视图)来格式化和显示HTML代码
控制器:
public function addPermissionFormAction()
{
$this->_helper->layout->disableLayout();
//data from ajax call is successfully received
$username = $this->getRequest()->getParam('user');
//...Code to get $pages...
$form = new Pds_Wizard_SubForm_Externals($pages);
if(!isset($form)) $form = false;
//I have run XDebug to here and confirmed that the form is built successfully.
echo Zend_Json::encode(array('form' => $form));
//Assign the form to a variable view
$this->view->form = $form;
}
查看:add-permission-form.phtml:
<?php echo $this->form ;?>
Ajax电话:
没有评论,你的代码看起来很酷