如何在magento中使用ajax

magento自帶了prototype這個JS框架,因爲prototype封裝了常用的ajax功能,所以magento也可以很方便的使用ajax。
jsfile.js 代碼如下,注意url是https的別忘了s
    var url = 'https://www.domain.net/your_module/your_controller/getdata/'
    function getalldata(url)
    {
       new Ajax.Request(url, {
            method: 'get',//'post'
            onSuccess: function(text) {
                yourtext = text.responseText.evalJSON(true);
                //process something
            },
            onFailure: function() {
                return false;
            },
            onException: function() {
                return false;
            }
        });

        return true;
    
    }
phtml中引入js文件
<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/jsfile.js') ?>"></script> 
php文件,/your_module/your_controller.php
    protected function getdataAction() {

        $mydata = array();
        //processing ... ...
        echo Zend_Json::encode($mydata);

    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章