2012-11-29 24 views
0

我需要了解如何在Yii中構建Ajax請求。我搜索了Yii的網站上,發現了下面的文章:Yii中的Bulid Ajax

http://www.yiiframework.com/wiki/24/

我寫的代碼,我測試了它在我的本地?但由於某種原因,它不起作用。

對於第一次嘗試,我只想做一些簡單的事情。我想通過使用Ajax在我的頁面上打印另一個動作的結果。我想要顯示的文字是'嗨'。

這是畝代碼看起來像那個動作:

視圖/索引

<?php 
/* @var $this CurrentController */ 

$this->breadcrumbs=array(
     'Current'=>array('/current'), 
     'index', 
); 
?> 
<div class="form"> 

<?php $form=$this->beginWidget('CActiveForm', array(
     'id'=>'users-index-form', 
     'enableAjaxValidation'=>true, 
)); ?> 
<?php 
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'), 
array(
'ajax' => array(
'type'=>'POST', //request type 
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call. 
//Style: CController::createUrl('currentController/methodToCall') 
'update'=>'#city_id', //selector to update 
//'data'=>'js:javascript statement' 
//leave out the data key to pass all form values through 
))); 

//empty since it will be filled by the other dropdown 
echo CHtml::dropDownList('city_id','', array()); 


?> 


<?php $this->endWidget(); ?> 

</div><!-- form --> 

控制器

<?php 



class CurrentController extends Controller 
{ 



public function accessRules() 
    { 
     return array(
      array('allow', // allow authenticated user to perform 'create' and 'update' actions 
       'actions'=>array('create','update','dynamiccities'), 
       'users'=>array('@'), 
      ), 
     ); 
    } 
public $country_id; 
    public function actionIndex() 
    { 
     $this->render('index'); 
    } 




public function actionDynamiccities() /// Called Ajax 
{ 


     echo CHtml::tag('option', 
        array('value'=>'2'),CHtml::encode('Text'),true); 

} 



} 

不幸的是我沒有得到期望的結果。我得到的是:

  1. drowpdown list contains country array。
  2. 另一個下降列表,但空??

我該如何解決我的示例代碼,以便它能工作?任何人都可以看到我做錯了什麼?

+0

代碼看起來不錯。你能檢查一下Firebug嗎?並看到真正的ajax請求觸發器。 – GBD

+0

GBD它的工作原理:查看我的結果: http://up.arabseyes.com/uploads/29_11_1213541819231.jpg –

+0

您是否在快照中看到了'response header'?它是404 – GBD

回答

0
echo CHtml::dropDownList('city_id','', array()); 

使用id作爲

echo CHtml::dropDownList('city_id','', array('id'=>'city_id')); 
+0

HEMC , 不行 :( –