简体   繁体   English

在ajax调用不起作用后,Yii从控制器操作重定向

[英]Yii redirect from controller action after ajax call not working

I use the following buttons in my view file (don't pay attention to second one, but I just wanted to show you why I'm not using a normal form submit): 我在视图文件中使用了以下按钮(不关注第二个按钮,但我只是想向您展示为什么我不使用常规表单提交):

    <?php echo CHtml::Button('Search Symptom(s)', array('id'=>'search'));  ?>
    <?php echo CHtml::Button('Add Another Symptom to Search', array('id'=>'addSymptom'));  ?>

When the user clicks the buttons this javascript runs (it's inside a document.ready function) 当用户单击按钮时,此javascript将运行(在document.ready函数内部)

$('#search').click(function()
        {   
            //create new symptom in javascript
            var newSymptom = 
            {
                symptomCode: $('#symptomToBeSearchedCode').val(),
                dateSymptomFirstSeen: $('#dateSymptomSeen').val(),
                symptomTitle: $('#symptomToBeSearchedTitle').val()
            };
            //pass new symptom into symptomsList array
            symptomsList.push(newSymptom);
            //make ajax call to server
            $.ajax({
                type:'POST',
                url: '/mysymptomsbook/index.php?r=symptomhistory/search',
                data:{symptomsList: symptomsList} ,
                dataType:'html'
            });
        });

symptomsList is an array with JS objects issuesList是一个包含JS对象的数组

This is the code in my controller action: 这是我的控制器操作中的代码:

 if(isset($_POST['symptomsList']))
    {   
        foreach($_POST['symptomsList'] as $symptom)
        {
            //populate symptom search model attributes with user id, current date, and form input
            $newSymptom = new Symptomhistory;
            $newSymptom->setAttributes(array(
                                'user_id'=>Yii::app()->user->id,
                                'dateSearched'=>date('Y-m-d'),
                                'symptomCode'=>$symptom['symptomCode'],
                                'dateSymptomFirstSeen'=>$symptom['dateSymptomFirstSeen'],
                                'symptomTitle'=>$symptom['symptomTitle'],
                                 ));
            //save search history
            $newSymptom->save();
            //add into the searched for symptoms code the latest code
            array_push($symptomCodes, strval($symptom['symptomCode']));
        }

        $this->redirect(array('disease/index'));
    }

I was planning on using redirect to send the $symptomCodes array to the other controlleraction (DiseasesController and actionIndex), but even without passing anything the redirect doesn't work. 我打算使用重定向将$ symptomCodes数组发送到其他controlleraction(DiseasesController和actionIndex),但是即使没有传递任何内容,重定向也不起作用。 The models get saved to my database normally. 这些模型通常会保存到我的数据库中。

Anyone have any idea what is wrong? 任何人都知道哪里错了吗? I'm thinking it has to do with Ajax since it's waiting for a response, but I want my controller to redirect instead. 我认为它与Ajax有关,因为它正在等待响应,但是我希望控制器重定向。 Any help as always, is greatly appreciated :) 一如既往的任何帮助,我们将不胜感激:)

I had similar problem, recommend you to look at this topic at official forum: redirect not working when called via Ajax-Request 我有类似的问题,建议您在官方论坛上查看此主题: 通过Ajax-Request调用时重定向不起作用

See the last answer in topic. 请参阅主题中的最后一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM