0

我在对话框关闭功能上使用 ajax 调用时遇到了这个问题。

这是我的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 调用也总是失败。我检查了我的参数,它还有要传递的适当字符串。我非常需要帮助。任何对我有帮助的评论都将受到高度赞赏。谢谢。(^__^)

4

1 回答 1

0

我认为你最好的办法是通过 ajax 函数修复你正在访问的 url:

在视图文件中

$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

完整的网址将是:

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",

希望这将有助于进一步阅读

于 2013-11-06T08:07:40.850 回答