shuba.ivan
shuba.ivan

Reputation: 4061

Symfony redirect(reload) to another page after success

I have ajax call and after call, when success I need redirect(reload) to another cation, example /test, how to do it ?

        $.ajax({
        type: "POST",
        url: '/app_dev.php/api/tasks',
        data: {
            ids: result,
            invoice_id: invoice_id
        },
        success: function (data) {
            // how to reload page in another path, example /app_dev.php/test
        },
        error: function () {
            // alert('it broke');
        },
        complete: function () {
            // alert('it completed');
        }
    });

Upvotes: 2

Views: 7390

Answers (4)

Graftak
Graftak

Reputation: 713

You can just use this javascript code to direct the user to another page on success: window.document.location = '/app_dev.php/test';.

In your code:

success: function (data) {
        // how to reload page in another path, example /app_dev.php/test
        window.document.location = '/app_dev.php/test';
    },

Having said that, you should not hard-code the 'app_dev.php' part of the url, as you probably already know, since this only works for the dev environment of Symfony.

You can define the 'prefix' web path based on whether the environment is set to 'prod' or 'dev' (or some other environment).

Upvotes: 0

You can't redirect from controller via ajax request, so you need to pass redirect path to the response and then

success: function(response) {
    window.location.href = response.yourPath;
}

Upvotes: 0

Mert Öksüz
Mert Öksüz

Reputation: 1071

You can use smyfony2 routing in javascript with FOSJsRoutingBundle

Steps;

  1. Install FOSJsRoutingBundle with https://symfony.com/doc/master/bundles/FOSJsRoutingBundle/installation.html
  2. Use your routings in your success function with https://symfony.com/doc/master/bundles/FOSJsRoutingBundle/usage.html

For example in your case;

<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script>
$.ajax({
        type: "POST",
        url: '/app_dev.php/api/tasks',
        data: {
            ids: result,
            invoice_id: invoice_id
        },
        success: function (data) {
            window.document.location = Routing.generate('your_test_route_name');
        },
        error: function () {
            // alert('it broke');
        },
        complete: function () {
            // alert('it completed');
        }
    });
</script>

Upvotes: 2

kapa89
kapa89

Reputation: 615

Try this

$.ajax({
    type: "POST",
    url: '/app_dev.php/api/tasks',
    data: {
        ids: result,
        invoice_id: invoice_id
    },
    success: function (data) {
        location.href = '/app_dev.php/test';
    },
    error: function () {
        // alert('it broke');
    },
    complete: function () {
        // alert('it completed');
    }
});

Also for better routing in JS you can look at FOSJsRoutingBundle

Upvotes: 3

Related Questions