magento自带了prototype这个JS框架,因为prototype封装了常用的ajax功能,所以magento也可以很方便的使用ajax。
/** * Created by PhpStorm. * User: ajiang-tuz * Date: 2015/5/18 * Time: 10:03 * Note ajax 测试 */ <script type="text/javascript"> var url = 'http://127.0.0.1/magento/weicot/index/ajax'; new Ajax.Request(url, { method: 'get', onComplete: function(response) { if(response.status == 200) { //判断返回状态 var result = response.responseText.evalJSON(); //返回一个josn 对象 {"date":"11\/24\/15 0923::49 PM"} }//结束 //直接获得 返回数据 var val = result.date; //11\/24\/15 0923::49 PM } });
phtml中引入js文件
<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/date.js') ?>"></script> php文件,/your_module/your_controller.php
//http://127.0.0.1/magento/weicot/index/ajax 前端控制器 <?php /** * Created by PhpStorm. * User: ajiang-tuz * Date: 2015/11/18 * Time: 10:03 * Note ajax 测试 */ class Aps_Weicot_IndexController extends Mage_Core_Controller_Front_Action { public function ajaxAction(){ $dataput=Date("m/d/y h:i:s A", strtotime(Mage_Core_Model_Locale::date(null, null, "en_US", true))); $data=array('date'=>$dataput); echo Zend_Json::encode($data); } }
参考 以及引用
<script type="text/javascript"> //<![CDATA[ //var dataForm = new VarienForm('form-validate', true); <?php if($this->getShowAddressFields()): ?> new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>); <?php endif; ?> $('create-account-button').observe('click', function(e) { Event.stop(e); var url = '<?php echo $this->getUrl('onestepcheckout/ajax/register', array('_secure'=>true)); ?>'; var parameters = $$('form.register-form').first().serialize(true); new Ajax.Request(url, { method: 'post', parameters: parameters, onComplete: function(response) { if(response.status == 200) { var result = response.responseText.evalJSON(); if(result.success && result.message == 'customer_logged_in') { window.location='<?php echo $this->getUrl('customer/account', array('_secure'=>true)); ?>'; } else { alert('Error occured: ' + result.error); } } } }); }); //]]> </script>
?>