yii 使用ajax详解

jerry Yii 2015年08月23日 收藏

客户端方法 

  1. function callService(addr,port,service,tabId)  
  2. {  
  3.     //检查数据的完整性  
  4.     if(0>getServiceInfo(addr,port,service))  
  5.     {  
  6.         return;  
  7.     }  
  8.       
  9.     var Addr = document.getElementById(addr).value;  
  10.     var Port = document.getElementById(port).value;  
  11.     var Service = document.getElementById(service).value;     
  12.          
  13.     var param = formatTableData(tabId,"dataset");      
  14.  
  15.         $.ajax({  
  16.                 url:"<?index.php/gearman/Ajax",   
  17.                 type : 'POST',  
  18.                 data : {curAddr:Addr,  
  19.                     curPort:Port,  
  20.                     curService:Service,  
  21.                     curParam:param},  
  22.                 dataType : 'text',  
  23.                 contentType : 'application/x-www-form-urlencoded',  
  24.                 async : false,  
  25.                 success : function(mydata) {  
  26.                         alert("success");  
  27.                         alert(mydata);  
  28.                         var show_data = "<h1>result:" + mydata + "</h1>";  
  29.                         $("#result").html(show_data);  
  30.                 },  
  31.                 error : function() {  
  32.                         alert("calc failed");  
  33.                 }  
  34.         });  
  35. }

服务端如何响应

  1. <?php  
  2.   
  3. class GearmanController extends Controller  
  4. {     
  5.     public function actionAjax()  
  6.     {  
  7.         $addr = Yii::app()->request->getParam('curAddr');  
  8.         $port = Yii::app()->request->getParam('curPort');  
  9.         $service = Yii::app()->request->getParam('curService');  
  10.         $param = Yii::app()->request->getParam('curParam');  
  11.           
  12.         echo $addr . $port . $service . $param;  
  13.   
  14.     }  
  15. }  
  16. ?>