ajax – 在自定义控制器操作中使用Magento内置消息系统
我正在调整我的magento商店,几乎一切都在游泳,除了一件事:
问题: 说明: 我试过的事情: $_messages = Mage::getSingleton("core/session")->getMessages(); echo $this->getLayout()->createBlock("core/messages")->setMessages($_messages)->getGroupedHtml(); echo $this->getMessagesBlock()->getGroupedHtml(); echo Mage::app()->getLayout()->getMessagesBlock()-> setMessages(Mage::getSingleton('customer/session')->getMessages(true))->getGroupedHtml(); Mage ::记录数据显示空消息集合. 这是布局XML(我使用$this-> loadLayout(‘ajax_msg_handle’);来自控制器):. <ajax_msg_handle> <block type="core/template" name="error.root" output="toHtml" template="page/html/ajax-messages.phtml"> <block type="core/messages" name="global_messages" as="global_messages"/> <block type="core/messages" name="messages" as="messages"/> </block> </ajax_msg_handle> 另外一个细节点,我考虑过以下几点: 大多数操作,如购物车的“删除”,“编辑”和产品页面的“添加到购物车”,首先重定向到另一个地方,因此会发出第二个请求,显示错误.也许这些消息永远不会立即显示,但只有在生成错误的请求之后才响应请求.所以我试图通过重定向到显示这些消息的动作来遵循这种可能的约定,但这也不起作用. 如果有人能告诉我如何让这些消息出现,它将成为我的一天. 解决方法
您尝试使用的消息块不适用于AJAX查询.正如您所指出的,它只出现在重定向之后,即:
>通过以下内容在会话中设置消息:Mage :: getSingleton(‘adminhtml / session’) – > addError(Mage :: helper(‘modulename’) – > __(‘error message’)); 为了直接在AJAX调用中显示此消息块(即,没有重定向),您必须: >在你的控制器动作中:设置消息并加载块: Mage::register('message',Mage::helper('yourmodule')->__('the error message'); $layout = $this->getLayout(); $update = $layout->getUpdate(); $update->load('ajax_msg_handle'); //loading your custom handle,defined in your module's layout .xml file $layout->generateXml(); $layout->generateBlocks(); $output = $layout->getOutput(); $this->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('error' => $output))); >在.phtml中回显消息: <ul class="messages"> <li class="error-msg"> <ul> <li><?php echo Mage::registry('message'); ?></li> </ul> </li> </ul> >在ajax调用的onComplete部分输出javascript代码中的响应: onComplete: function(transport) { $$('.main-col-inner')[0].insert({before:transport.responseText.evalJSON().error}); Element.hide('loading-mask'); } 请注意,最好使用自己的块类型来扩展Mage_Core_Block_Messages并在该块的方法中管理消息,而不是使用注册表.希望有所帮助 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |