Magento – How to Detect Redirect Response in Ajax Using Prototype.js

ajaxprototype-jsredirect

I'm using Prototype.js to send a post request to a Magento controller. The controller will response a Json data or a redirect e.g. $this->_redirect('*/*/billing');
The issue that it seems the Ajax cannot detect the redirect response. I need to "catch" it then handle the redirect action in JavaScript by using window.location = redirect_url;
Please help me as I don't want a workaround solution such as modifying response behavior in the controller. Thanks so much!.

new Ajax.Request(orderSaveUrl, {
    method: 'post',
    parameters: params,
    onComplete: function (response) {
        window.console.log('success' + response);
    },
    onFailure: function (response) {
        window.console.log('failure' + response);
    }
});

Best Answer

The browser handles the HTTP redirect internally, but I think you can figure out what the final URL was by inspecting the ajax transport object.

new Ajax.Request(orderSaveUrl, {
    method: 'post',
    parameters: params,
    onComplete: function (response) {
        window.console.log('success' + response);
        window.console.log(response.transport.responseURL); // <=====
    },
    onFailure: function (response) {
        window.console.log('failure' + response);
        window.console.log(response.transport.responseURL);
    }
});
Related Topic