0

After ajax success can't redirect page in admin in Magento 2

 var redirectUrl = "<?php echo $block->getBaseUrl().'admin/trackorder/shipinfo' ?>";
 $.ajax({
    url: urlLabel,
    type: "POST",
    data: {counsumerNumber:counsumerNumber,courierService:courierService},
    showLoader: true,
    done: function(data, statusText, xhr){
        var status = xhr.status;
        if(status != 200) window.location = redirectUrl;
    }
});

3 Answers 3

2
+50

Try this.

var redirectUrl = "<?php echo $this->getUrl('trackorder/shipinfo/index/'); ?>";
     $.ajax({
        url: urlLabel,
        type: "POST",
        data: {counsumerNumber:counsumerNumber,courierService:courierService},
        showLoader: true,
        done: function(data, statusText, xhr){
            var status = xhr.status;
            if(status != 200) window.open(redirectUrl, '_blank');
        }
    });
0

Try this code to redirect on page after success

var redirectUrl = "<?php echo $block->getUrl("trackorder/shipinfo"); ?>";
$.ajax({
    url: urlLabel,
    type: "POST",
    data: {counsumerNumber:counsumerNumber,courierService:courierService},
    showLoader: true,
    done: function(data, statusText, xhr){
        var status = xhr.status;
        if(status != 200) window.location.href = redirectUrl;
    }
});

Just change window.location = redirectUrl; to window.location.href = redirectUrl;

and <?php echo $block->getBaseUrl().'admin/trackorder/shipinfo' ?> to <?php echo $block->getUrl("trackorder/shipinfo"); ?>

5
  • After makes changes as you told, still I m getting redirects to the same page refer here awesomescreenshot.com/video/1763419 Nov 18, 2020 at 11:30
  • Video is private, URL not working. What is your current page URL and what is the new URL that you need? Nov 18, 2020 at 11:59
  • current page url 'trackorder/readytopack/index', new page URL 'trackorder/shipinfo/index', Refer this public URL awesomescreenshot.com/video/… Nov 18, 2020 at 12:17
  • Have you tried this? <?php echo $block->getUrl("trackorder/shipinfo/index/"); ?> Nov 18, 2020 at 12:27
  • yes, I tried with <?php echo $block->getUrl("trackorder/shipinfo/index/"); ?> but still redirect to same page Nov 18, 2020 at 12:35
0

In admin, getBaseUrl & getUrl don't always work if you have Add Secret Key to URLs on in the config.

Try This

var redirectUrl = '<?= /** @noEscape */ $block->getAdminBaseUrl()?>'+'trackorder/shipinfo';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.