redirect does not work in ajax function

The AJAX request runs in the background. Redirects here do not affect the main page. And 302 is not an error, it is just a status code.

Your AJAX response should return either the URL and the status code to the calling page or just a number like 1. Then you handle the redirect in the calling page:

jQuery( document ).ready( function( $ ) {
    var url="<?php echo home_url(); ?>";
    $( '#ajaxtrigger' ).on( 'click',
        function() {
            $.post( ajaxurl, {}, function( response ) {
                if ( 1 == response )
                    top.location.replace(url);
            });
            return false;
        }
    );
});

Leave a Comment