2011-09-28 155 views
1

我是Yii PHP框架的新手,請耐心等待。Yii異步jsonp請求

我需要在Yii應用程序數據庫中創建跨域JSONP請求(來自非yii應用程序)以創建記錄。在創建它應該通過getVisit返回應用程序/ JSON內容

的控制器:

public function actionGetVisit($id) 
{ 
    header('Content-type: application/json'); 

    $visit = Visit::model()->findByPK((int)$id); 

    echo CJSON::encode($visit); 

    Yii::app()->end(); 
} 
/** 
* Creates a new model. 
* If creation is successful, the browser will be redirected to the 'view' page. 
*/ 
public function actionCreate() 
{ 
    $model=new Visit; 

    // Uncomment the following line if AJAX validation is needed 
    // $this->performAjaxValidation($model); 

    if(isset($_POST['Visit'])) 
    { 
     $model->attributes=$_POST['Visit']; 
     if($model->save()) 
      $this->redirect(array('getVisit','id'=>$model->id)); 
    } 

    $this->render('create',array(
     'model'=>$model, 
    )); 
} 

的形式爲:

<form id="visit-form" action="http://host/index.php?r=visit/create" method="post"> 
      <p class="note">Fields with <span class="required">*</span> are required.</p> 


      <div class="row"> 
       <label for="Visit_rvc_id" class="required">Rvc <span class="required">*</span></label>  <input name="Visit[rvc_id]" id="Visit_rvc_id" type="text" value="1">   </div> 

      <div class="row"> 
       <label for="Visit_zone" class="required">Zone <span class="required">*</span></label>  <input name="Visit[zone]" id="Visit_zone" type="text" value="1">   </div> 

      <div class="row"> 
       <label for="Visit_table" class="required">Table <span class="required">*</span></label>  <input name="Visit[table]" id="Visit_table" type="text" value="1">   </div> 

      <div class="row"> 
       <label for="Visit_seat" class="required">Seat <span class="required">*</span></label>  <input name="Visit[seat]" id="Visit_seat" type="text" value="1">   </div> 

      <div class="row"> 
       <label for="Visit_user_id" class="required">User <span class="required">*</span></label>  <input name="Visit[user_id]" id="Visit_user_id" type="text" value="1">   </div> 

      <div class="row"> 
       <label for="Visit_guest_name" class="required">Guest Name <span class="required">*</span></label>  <input size="60" maxlength="256" name="Visit[guest_name]" id="Visit_guest_name" type="text" value="1">   </div> 

      <div class="row"> 
       <label for="Visit_created" class="required">Created <span class="required">*</span></label>  <input name="Visit[created]" id="Visit_created" type="text" value="1">   </div> 

      <div class="row buttons"> 
       <input type="submit" name="yt0" value="Create"> </div> 

     </form> 

的JS:

$('#visit-form').submit(function(event) 
     { 
      alert('submit'); 
      event.preventDefault(); 
      var $form = $(this); 
      $.ajax({ 
       url: $(this).attr('action'), 
       dataType: 'jsonp', 
       type: 'POST', 
       data : $form.serialize()+'&ajax='+$form.attr('id'), 
       success: function(data, textStatus, XMLHttpRequest) 
       { 
        alert('success'); 
        if (data != null && typeof data == 'object'){ 
         $.each(data, function(key, value){ 
          $('#error').append(value); 
         }); 
        } 
       }, 
       error: function(XMLHttpRequest, textStatus, errorThrown) 
       { 
         alert(errorThrown); 
         alert('error'); 
       } 
      }); 
      return false; 
     }); 

在提交: 它看起來像它沒有錯誤或成功。響應說:

GET http://host/index.php?r=visit/create&callback=jQuery15102636089683510363_1317230765087&Visit%5Brvc_id%5D=1&Visit%5Bzone%5D=1&Visit%5Btable%5D=1&Visit%5Bseat%5D=1&Visit%5Buser_id%5D=1&Visit%5Bguest_name%5D=1&Visit%5Bcreated%5D=1&_=1317230785272 The URL can’t be shown 

的響應被設置爲輸出文本/

有誰知道這是什麼錯誤意味着什麼?表單提交完全沒有js。但我似乎不能得到Ajax請求的工作。我將它設置爲'jsonp',這樣跨域問題就會消失。但我不確定Yii後端是否可以處理作爲jsonp發送的數據。任何幫助表示讚賞!

回答

3

這不是一個真正的Yii問題,更多的是JSONP問題;這裏是你的GetVisit功能應該是什麼樣子:

public function actionGetVisit($id) 
{ 
    header('Content-type: application/json'); 

    $visit = Visit::model()->findByPK((int)$id); 

    $json = CJSON::encode($visit); 
    echo $_GET['callback'] . ' (' . $json . ');'; 

    Yii::app()->end(); 
} 

的jQuery附加一個全局臨時函數時JSONP請求中插入腳本被稱爲窗口對象。 jQuery取代了?與生成的函數名稱(即jsonp1232617941775)調用內聯函數。您正在將該函數傳遞給窗口對象。

希望有幫助,如果沒有正確解釋我正在進行項目工作,請道歉。