2013-11-05 53 views
0

我有使用ajax調用對話框關閉功能的這個問題。Ajax調用yii對話框關閉功能失敗

這是我的Ajax調用函數:

function samplefunction(var1,var2){  

    $.ajax({ 
     url: "controllerFunction?var1="+var1+"&var2="+var2, 
     type: 'POST', 
     dataType: 'json', 
     success: function(data) 
     { 
      $("#htmlmessage").html(data.message); 
      $("#htmlmsgdialog").dialog("open"); 
     } 
    }); 
} 

這裏的對話框代碼:

<?php 
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
    'id'=>'sampledialogboxname', 
    'options'=>array(
     'title'=>'Sample Dialog Box I', 
     'autoOpen'=>false, 
     'modal'=>true, 
     'resizable'=>false, 
     'draggable'=>false, 
     'position'=>array("middle",30), 
     'width'=>650, 
     'show'=>'fade', 
     'hide'=>'fade', 
     'open' => 'js:function(event,ui){ 
         //some code here 
     }', 
     **'close' => 'js:function(event,ui){ 
         samplefunction("samplestring1","samplestring2"); 
         window.location.href = "'.$sampleurl.'"; 
     }',** 
     'buttons' => array 
     (
      array('id' => 'firstback','text'=>'BACK', 
         'click'=> 'js:function(){ 
            samplefunction("samplestring1","samplestring2"); 
            $(this).dialog("close"); 
            window.location.href = "'.$sampleurl.'"; 
         }'), 
      array('id' => 'secondback','text'=>'BACK','click'=> 'js:function(){ 
            //some code here 
         }'), 
      array('id' => 'thirdback','text'=>'BACK','click'=> 'js:function(){ 
            //some code here 
         }'), 
      array('id' => 'fourthback','text'=>'BACK','click'=> 'js:function(){ 
            //some code here 
         }'), 
      array('id' => 'firstnext','text'=>'NEXT','click'=> 'js:function(){ 
            //some code here 
         }'), 
      array('id' => 'secondnext','text'=>'NEXT','click'=> 'js:function(){ 
            //some code here 
         }'), 
      array('id' => 'thirdnext','text'=>'NEXT','click'=> 'js:function(){ 
            //some code here 
         }'), 
      array('id' => 'save','text'=>'SAVE','click'=> 'js:function(){ 
            //some code here 
         }') 
     ), 
    ), 
));?> 

這裏的控制器功能:

public function actionControllerFunction($var1, $var2) 
{ 
    var_dump($var1, $var2); 
    //Do Some Code here 

    $result['showdialog'] = true; 
    $result['message'] = "Sample Msg."; 

    echo json_encode($result); 
    exit; 
} 

我的問題是,AJAX調用總是失敗甚至在我進入控制器功能之前。 我檢查了我的參數,它也有適當的字符串傳遞。 我非常需要幫助。任何意見,這將有助於我對此表示高度讚賞。 謝謝。 (^ __ ^)

+0

你的意思是Ajax調用沒有發生? ajax呼叫的狀態是什麼? –

+0

我沒有看到任何返回的狀態,只是網址在螢火蟲中處於紅色狀態,並且沒有任何操作。 (_。_) –

+0

您檢查螢火蟲請求的響應 –

回答

0

我覺得你最好的辦法是解決您正在訪問雖然AJAX功能的網址:

鑑於文件

$sampleurl=Yii::app()->createUrl("controllerName/sampleFun"); 
$ajaxFun=Yii::app()->createUrl("controllerName/controllerFunction"); 
$ajaxReq = <<<JS 
function samplefunction(var1,var2 ,dlg,refreshUrl){  

    $.ajax({ 
     url: "$ajaxFun&var1="+var1+"&var2="+var2, 
     type: 'POST', 
     dataType: 'json', 
     success: function(data) 
     { 
      $("#htmlmessage").html(data.message); 
      $("#htmlmsgdialog").dialog("open"); 
     } 
    }); 

    if(dlg!=null) $(dlg).dialog('close'); 
    //window.location.href=refreshUrl 
} 
JS; 

注意的網址:url: "$ajaxFun&var1="+var1+"&var2="+var2,

給上下文控制器是SiteController它的ajax url應該是這樣的:

site/controllerFunction&var1=abc&var2=def

完整的URL將是:

index.php?r=site/controllerFunction&var1=abc&var2=def

你的情況

你錯把

index.php?r=site/controllerFunction?var1=abc&var2=def

注意其中兩個將是明顯錯誤的(?)。

一個建議:

  1. 通過滴速參數

    public function actionControllerFunction(){ //use $_POST array .... }

  2. 發送該數據作爲從AJAX方法 功能samplefunction後數據(VAR1,VAR2修復功能, dlg,refreshUrl){

    $.ajax({ 
        url: "$ajaxFun", 
        data:"&var1="+var1+"&var2="+var2", 
        type: 'POST', 
        dataType: 'json', 
        success: function(data) 
        { 
         $("#htmlmessage").html(data.message); 
         $("#htmlmsgdialog").dialog("open"); 
        } 
    }); 
    

    url: "$ajaxFun",data:"&var1="+var1+"&var2="+var2",

希望這將有助於read further