【问题标题】:Yii ajax loads whole page instead of my outputYii ajax 加载整个页面而不是我的输出
【发布时间】:2014-02-17 13:20:00
【问题描述】:

我在使用 ajax 的 YII 中遇到问题。 我使用this link 作为例子来测试我的项目中的ajax 依赖下拉列表。

表单代码(protected/views/game/_form.php):

<?php 
    echo $form->dropDownList($model, 'season_id', Season::getSeasonsList(), array(
        'ajax' => array(
            'type'=>'POST',
            CController::createUrl('Game/selectGameStages'),
            'update'=>'#Game_season_game_stage'
            )
        )
    ); 
?>

控制器代码(protected/Controller/GameController.php):

public function actionSelectGameStages()
{
    echo CHtml::tag('option', array('value'=>'1'), 'Some output 1', true);        
    echo CHtml::tag('option', array('value'=>'2'), 'Some output 2', true);        
    echo CHtml::tag('option', array('value'=>'3'), 'Some output 3', true);        
}

Ajax 正在工作,但如果使用调试器创建该代码,则放入我的 Game_season_game_stage 选择是我网站整个页面的 html 代码,如

<select name="Game[season_game_stage] id="Game_season_game_stage">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    ...
    <div class="container" id="page">...</div>

请帮助理解为什么加载整个页面代码而不是我的 selectGameStages 函数中的代码? Yii 版本是 1.14。 对不起我的英语不好。谢谢。

【问题讨论】:

  • 请添加您要更新的代码。

标签: ajax yii


【解决方案1】:

输出后直接退出。

public function actionSelectGameStages()
{
    // Do stuff
    ...

    exit;
}

【讨论】:

    【解决方案2】:

    为什么不直接在视图中使用 dropDownList ?

    es.

    <?php echo CHtml::dropDownList('listname', $select, 
                  array('M' => 'Male', 'F' => 'Female'),
                  array('empty' => '(Select a gender)'));
    

    http://www.yiiframework.com/wiki/48/by-example-chtml/#hh5

    【讨论】:

    • 我需要根据从第一个下拉列表中选择的值从其他表中生成选项
    【解决方案3】:

    感谢您的所有回答!我找到了解决方案。

    在 protected/views/game/_form.php 我错过了“url”键并重命名了游戏/selectGameStages

    <?php 
        echo $form->dropDownList($model, 'season_id', Season::getSeasonsList(), array(
            'ajax' => array(
                'type'=>'POST',
                'url' => CController::createUrl('game/selectGameStages'),
                'update'=>'#Game_season_game_stage'
                )
            )
        ); 
    ?>
    

    之后我用这个代码更新了控制器代码(protected/Controller/GameController.php)

    public function accessRules()
        {
            return array(
                array('allow',  // allow all users to perform 'index' and 'view' actions
                    'actions'=>array('index','view', 'selectGameStages'),
                    'users'=>array('*'),
    

    感谢您的帮助!

    【讨论】:

      【解决方案4】:

      由于您的布局,您的整个页面正在加载,

      在你的控制器中检查这个调用是否是一个 ajax 调用,将你的布局更改为没有这些标签的布局,或者更好地为你的 ajax 调用创建一个布局,

      if($this->getIsAjaxRequest())
            $this->layout = '//ajax'; // ajax layout
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-05
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多