【发布时间】:2009-07-07 19:11:30
【问题描述】:
你好,在斯塔克兰!这是我的问题:
我想使用我的 Zend 控制器从数据库中加载一个数组,然后将它传递给 javascript。我决定最好的方法是使用 ajax 向控制器询问它的数组,将其编码为 json,然后将其传递下来。但是,我不知道如何将我在第一个操作中加载的变量传递给通过 ajax 调用它时将其传递下来的操作。
产生视图的原始动作
public function indexAction()
{
$storeid = $this->getStoreId();
if(!$storeid)
{
$this->_forward('notfound');
return;
}
$store = $this->_helper->loadModel('stores');
$store->getByPrimary($storeid);
}
将通过 ajax 调用的操作
public function getdataAction()
{
$this->_helper->Layout->disableLayout(); // Will not load the layout
$this->_helper->viewRenderer->setNoRender(); //Will not render view
$jsonResponse = json_encode($store);
$this->getResponse()->setHeader('Content-Type', 'application/json')
->setBody($jsonResponse);
}
我想要的是将 indexAction 中的 $store 传递给 getdataAction,以便它可以将 store 作为 jsonResponse 发送。请注意,它们在两个不同的时间被调用。
我尝试过但没有奏效的方法:
在 indexAction 中设置 $this->getRequest()->setParam('store', $store),然后在 getdataAction 中使用 $this->getRequest()->getParam('store')。我认为这不起作用,因为它们是不同的 http 请求,所以附加一个新参数是没用的。
在控制器本身中使用受保护的 $_store,然后使用 indexAction 保存到它,并在 getdataAction 中使用它。我不确定为什么这不起作用。
有没有一种以这种方式传递变量的好方法?有没有办法在不同的控制器之间传递变量?(我假设一个的答案是另一个的答案)。我可以将它存储在控制器助手中吗?我是否必须使用我知道会起作用但似乎没有必要的会话?有没有更好的方法将变量传递给javascript?我问的问题太多了吗?任何帮助都会非常出色。谢谢。
【问题讨论】:
标签: php javascript jquery zend-framework