防止zend controller / ajax刷新页面

时间:2012-09-25 22:45:00

标签: php ajax zend-framework

我的页面有一个下拉选择框。当选择某些东西时,会有一个调用zend控制器的ajax调用,该控制器返回一些数据,然后页面的另一部分被数据填充。问题是页面刷新,我想阻止它。

到目前为止,我已经尝试了几项不起作用的事情。

推出

event.preventDefault();
return false;

在我的javascript / jquery / ajax

$this->disableRender();
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);

在控制器本身..并且还放

return false; 

在控制器的末尾。

有人有任何建议吗?

编辑:这是下面的ajax和控制器代码

<script>

$(document).ready(function() {

    $("#advDropdown").change(function (event) {
        //event.preventDefault();
        //return false;
        var advertiser_id;
        advertiser_id = $("#advDropdown").val();

        $.ajax({
            type: 'POST',
            dataType: 'json',
            //url: '/dashboard/reporting/getallcampaigns',
            url: '/admin/ajax/getallcampaigns?format=json',
            data: { advertiser_id: advertiser_id },
            success: function(data) {
                //alert(adv_data);
                //var data = jQuery.parseJSON(adv_data);
                var count = 0;
                // loop through the returned data object
                for(var key in data) {
                    if(typeof data[key] === "object") {
                        for(var i = 0; i < data[key].length; i++) {
                            for(var property in data[key][i]) {
                                 //alert(property + " = " + data[key][i][property]);
                                if(count == 0){

                                    $('#campaign').html('<table id="campaign">');
                                    $('#campaign').append('<thead><tr><th width="100"><input type="checkbox"></th><th width="500">Campaign Name</th>');
                                    $('#campaign').append("</tr></thead>");

                                } else {
                                     $('#campaign').append('<tr><td><input type="checkbox" id=' + property +'"></td><td>' + property + " - " + data[key][i][property] + "</td></tr>" );

                                }
                                count = count + 1;
                            }
                        }
                        $('#campaign').append("</table>");

                    } else if(typeof data[key] === "string") {
                       // alert(key + " = " + data[key]);
                    }
                }

                //alert("campaigns: " + campaigns);
                //this.preventDefault();
                event.preventDefault();
                return false;


            },
            error: function() {
                alert("could not change the status indicator color");
            }
        });  

        return false;  
    });

</script>

以下是控制器代码

<?php

class Admin_AjaxController extends Zend_Controller_Action{

    public function init(){

        $this->_helper->layout->disableLayout();

        $this->_helper->viewRenderer->setNoRender();
    }

    public function getallcampaignsAction(){


            $this->_helper->layout->disableLayout();

            $this->_helper->viewRenderer->setNoRender();


            $campaign = new Campaign();

            $data = $this->_request->getPost();

            $campaigns = $campaign->getCampaigns($data['advertiser_id']);



            $result = array();

            $db = Zend_Db_Table_Abstract::getDefaultAdapter();
            foreach($campaigns as $campaign){

                // get campaigns id and get the name

                $c_sql = 'SELECT * FROM campaigns WHERE ext_id =  ' . $campaign['ext_id'];
                error_log("c_sql2: " .   $c_sql );


                $campaign_name = $db->query($c_sql)->fetchAll();

                $campaign_name2 = $campaign_name[0]['name'];



                $temp_array = array( $campaign['ext_id'] => $campaign_name[0]['name'] );
                array_push($result, $temp_array );


            }
            $final_array = array('data' => $result);




            //header('Content-type: application/json');

            echo json_encode($final_array);
            //Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer')
            //$this->_helper->sendJson($data, array('keepLayouts' => false));
            //$this->_helper->json($data, true, array('keepLayouts' => false);

            //error_log("");
            //return $final_array;
            //$response = $this->_helper->autoCompleteDojo->sendAutoCompletion($data);

            //$view = new ViewModel();
            //$view->setTerminate(true);
            //return $view;
            //return $this->response;
            //exit();

            //return false;
            //die();


        }

}


?>

1 个答案:

答案 0 :(得分:1)

我为ajax创建了一个名为AjaxController.php的控制器

public function init()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
}



private function __isAjax() {
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}

public function onselectAction(){
    if (self::__isAjax()) {
        if ($this->getRequest()->isPost()){
            $selected = $this->getRequest()->getPost('selected');
            $getModel = new Application_Model_DbTable_City();
            $this->view->data = $getModel->getData($selected);
            $this->renderScript('ajax/result.phtml');
        }

    }else{
        echo "some error occured";
    }
}

我通过这个jquery

调用了这个函数
jQuery.ajax({
            type: "POST",
            url: "<?php echo $this->url(array('controller'=>'ajax','action'=>'onselect'),null,true) ?>",
            data: 'selected='+selected,
            success: function(data) {
                jQuery('#result').html(data);
            }
        });

这对我来说很好。