AJAX是一项很棒的技术,用于改善用户体验并避免页面重新加载,但是如何在自己的Magento模块中使用它?让我们来探索……
1)您应该创建一个控制器,例如:Namespace / YourModule / controllers / AjaxController.php或在现有控制器中创建一个新动作
2)要在控制器中创建一个动作,例如indexAction(),您将添加此代码
1 2 3 4 5 | public function indexAction() { $this->loadLayout(); $this->renderLayout(); } |
3)然后添加到yourlayout.xml(例如:app / design / frontend / … / … / layout / yourlayout.xml)下一个代码
1 2 3 | <yourmodule_ajax_index> < block type = “yourmodule / yourblock” name = “root” template = “path / template.phtml” /> </ yourmodule_ajax_index > |
其中yourmodule是前端路由器,ajax是控制器名称,索引是操作名称
4)然后你需要更新config.xml中的布局
1 2 3 4 5 6 7 | < layout > < updates > < yourmodule > < file > yourlayout.xml </ file > </ yourmodule > </ updates > </ layout > |
5)在此之后,您的YourBlock类的实例将在template.phtml中可用
6)最后,您需要使用JavaScript或JQuery调用Ajax控制器,例如:
1 | xmlhttp.open(“GET”,“yourmodule / ajax / index / someval /” + value,true); |
要么
1 | jQuery.post(“yourmodule / ajax / index”,{ someval:value},function(data) {...}); |
您可能还想阅读: