2013-11-05 51 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