Yii中使用ajax例子

/*

*

*当input失去焦点时调用jquery中的jquery.ajax()

*/

$('#User_name').bind('blur',function(){
 
 //alert($(this).val());
 var id = "1";
 var val = "huangjiyun";
 $.ajax({

  type:"POST",
  dataType:"json",
  data:{"id":id,"val":val},
  url:"<?php echo Yii::app()->request->baseUrl;?>/index.php/user/Ajax",    //特别注意的地方, 需要将yii的urlmanager配置才能成功,不要使用index.php?r=user/Ajax,会报错
  success:function(json) {

   alert(json.val);

   },
  error:function(jqXHR, textStatus, errorThrown){
 
   alert(this.url);
   },
  statusCode:{
   404:function(){alert('not found page');} 
 }

});

 

//然后在UserController.php里加入一个新的动作来响应ajax中的url参数

public actionAjax(){

  

 
  if(Yii::app()->request->isAjaxRequest)

     {

      $id = (int)Yii::app()->request->getParam('id');
      $val = Yii::app()->request->getParam('val');
      echo CJSON::encode(array("val"=>$val));
       }

}

 

 

 

posted on 2013-01-29 23:44  alivewei  阅读(1223)  评论(0编辑  收藏  举报

导航