yii2 ajax request redirect not working

ajax request to redirect
jquery post not redirecting
ajax redirect
redirect back in ajax
ajax redirect callback
ajax intercept redirect
ajax header redirect
ajax redirect function

I tried to redirect the user after he click on a button. But the redirection is not working. This is my js code for handling the button click.

$('#MyButton').click(function() {
  $.ajax({
  url: '<?php echo Url::to(['configurator/compare']) ?>',
  type: 'POST',
  data: {
    Ref : Ref,
    searchname: $("#searchname").val() , 
    searchby:$("#searchby").val() , 
    _csrf : '<?=Yii::$app->request->getCsrfToken()?>'
  },
  success: function (data) {
  }
});

The http request image

The request is executed but i'm not redirected to the url. I stay on the same page when i click in the button.

The code you have posted does not perform a redirect.

All it does is make an ajax POST request to the url: ('configurator/compare') and passes the parameters in data:

To perform a redirect you could do it in the success: callback, e.g.

success: function (data) {
    window.location.href = 'http://google.com';
}

You can either hardcode the URL in above, or base it on the output of the script you posted to, which is available by accessing data inside the success callback.

redirect not working when called via Ajax-Request, Hi, I got the following code in my App: if(Yii::app()->request->isAjaxRequest) $this ->redirect( array('order/xxx')); else $this->redirect(� Ajax request to Yii2 not working #8047. dbd5 opened this issue Apr 12, 2015 · 3 comments Comments. Copy link Quote reply dbd5 commented Apr 12, 2015. Hello,

If you don't want return any data to you 'success' function, than in your controller 'configurator', in method 'compare', after handling your data you can make redirection to 'view' which you want:

    $data = Yii::$app->request->post();
    // do what you want with data
    return $this->redirect(['controller/action']);

Ajax Requests redirect to the page � Issue #103 � codemix/yii2 , It's not working. It still moves the browser to the ajax page. And, I don't want to manually have to include 25 AJAX calls into this list if I� The Ajax request is generated from JQuery. Expected behavior is an ajax request to the server which returns JSON to the same page. Actual behavior is the browser jumps to the url of the request page and displays the JSON result e.g. true or false. It repros on all Ajax requests as I described above. Completely breaks the use of Ajax.

Try Below way.

<script>
$(document).ready(function(){
     $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
     $.ajax({
            type:"POST",
            data:{id:id},
            url : '<?php echo Yii::$app->getUrlManager()->createUrl("/property/favourite"); ?>',
            success: function(response){
                        window.location.href = '<?php echo Yii::$app->getUrlManager()->createUrl("/site/chat"); ?>';        
                    }
    });

});
</script>

AJAX mode redirection logic is not RFC conform � Issue #64 � yiisoft , The current implementation of redirection logic in AJAX mode is not RFC conform . GitHub is home to over 50 million developers working together to host on my Yii2 app which response with a redirect the request will fail. redirect() Redirects the browser to the specified URL. yii\web\Controller: refresh() Refreshes the current page. yii\web\Controller: render() Renders a view and applies layout if available. yii\base\Controller: renderAjax() Renders a view in response to an AJAX request. yii\web\Controller: renderContent() Renders a static string by applying a

You can set the path in the controller and send the url back to the page.

public function actionAjaxMethod() {
    //my logic
    //if everything is successfulget the url path you want to redirect to.
    $url = Yii::$app->getUrlManager()->createUrl("/controllerName/methodName");
   return \yii\helpers\Json::encode([
      'status' => true,
      'url' => $url,
   ]);
}

In your view page all you do is

success: function (data) {
    if (data.status) {
         //redirect the page to url.
         window.location.href = data.url;
    } 
},

Runtime responses, When the current request is an AJAX request, sending a To solve this problem, the [[yii\web\Response::redirect()]] method� .htaccess | Too many redirects. apache,.htaccess,mod-rewrite,redirect. I'm assuming that you are trying to do two things here: Force HTTPS and www. Redirect to test.php if a certain IP is making the request The issue you were facing was due to the fact that even though the IP address was being matched, it was still being checked

Redirect to new page after jQuery AJAX call is successful (completed), Here Mudassar Ahmed Khan has explained how to redirect to new page (another page) after jQuery AJAX call is successful (completed) i.e.� yii2 documentation: Pjax advanced usage. Example. Yii Framework 2.0 ships with built-in support for Pjax, a JavaScript library that reduces page load times.It accomplishes this by only updating the part of the page that has changed through Ajax, which can translate into substantial savings if you have many other assets on your pages.

Problem with Ajax form post : on success redirect and show message, I have been converting my forms to submit via Ajax which is very convenient with validation since I get back validation error messages and� Sadly, I can not view Ajax request. It seems that only standard request show up in the log. I would expect that under Requests/Last 10, I could go back to an Ajax POST request. Is there a way to do that? ===== Update: The accepted answer is correct. But, it turned out that my problem was elsewhere.

Yii2 Modal Window for Create/Update/Delete with AJAX [CIRC Wiki], Yii::t('app', 'Loading Data'); // Loading If it is open, just reload content, not whole modal. // Also, this If update is successful, the browser will be redirected to the 'view' page. Creates a new model as an AJAX request. Ajax Request; Asset management; Components; Cookies; Custom Validations; Database Migrations; File Uploads; Installing Extension Manually; Pjax; Restful API; Routing and URLs; Session; Testing; Validation; Working with Databases; Yii2 ActiveForm; Yii2 Jquery Calendar For Text Field; Yii2 OAuth2 - Ex: consumer facebook OAuth2; Add auth action

Comments
  • You aren't actually redirecting anywhere though. It's just making an ajax POST request to a given URL.
  • But will i be able to access the post data in my controller through the redirection ?
  • I don't think you understand the flow of an ajax request. Your script makes a POST to 'configurator/compare'. At that point, you can access the data you have posted via ajax (e.g. searchname:, searchby:). You can return anything from that script which will then be readable in the success: callback. At that point you can do redirection, or anything else. But you can't do a PHP redirect because the request is being made with ajax, so that won't work.
  • but your ajax request will hit the error function because it cannot process the data it is receiving which is not a json response