JQuery Ajax Asynchronous Redirection












0















I face a weird issue while redirecting after an asynchronous ajax POST request. I want to send an asynchronous POST request (file upload) to the server and until I wait for it to finish, since it takes some time, I want to redirect to a different route and there I can watch the status of the upload. My problem is that even if I state that the request is async it first waits for this request to finish, synchronously and then redirects me.
Am I missing anything by chance?



upload.save = function (data) {
var self = this,
result = self.uploadType.getSummaryData();

$.ajax({
method: 'POST',
url: Routing.generate('save'),
data: JSON.stringify({
//My data
}),
cache: false,
contentType : 'application/json',
dataType: 'json',
context: self
});

// Redirection
setTimeout(function () {
window.location.replace(Routing.generate('log'));
}, 3000);
};


I am using Symfony 3.4 and JQuery version is 2.1 and for the redirect I use the FOS JS Routing Bundle v~1.4










share|improve this question




















  • 1





    That's not possible, if your page redirects, then your upload will be canceled, unless you are doing client side routing.

    – Iwan Wijaya
    Nov 19 '18 at 13:26











  • @IwanWijaya Yep, the redirection is client side. My question is, isn't it possible to just redirect to this route, while the upload goes on? why does it wait for the 'save' route to finish?

    – Kiriakos Papachristou
    Nov 19 '18 at 13:33













  • Simple answer is you can't replace the window the request is being made in without aborting the request. Client side routing is completely different ...think "single page app".

    – charlietfl
    Nov 19 '18 at 13:46













  • You could accept the data and let background process run on server processing the data update sesssion with progreess details. Then initial request would be done very quickly and you can poll another script for progress updates

    – charlietfl
    Nov 19 '18 at 13:49













  • Ok, so in any case the save POST request should be finished on the client side for the redirection to happen, just on the same time I can create a process on the server that the client doesn't care about

    – Kiriakos Papachristou
    Nov 19 '18 at 13:54
















0















I face a weird issue while redirecting after an asynchronous ajax POST request. I want to send an asynchronous POST request (file upload) to the server and until I wait for it to finish, since it takes some time, I want to redirect to a different route and there I can watch the status of the upload. My problem is that even if I state that the request is async it first waits for this request to finish, synchronously and then redirects me.
Am I missing anything by chance?



upload.save = function (data) {
var self = this,
result = self.uploadType.getSummaryData();

$.ajax({
method: 'POST',
url: Routing.generate('save'),
data: JSON.stringify({
//My data
}),
cache: false,
contentType : 'application/json',
dataType: 'json',
context: self
});

// Redirection
setTimeout(function () {
window.location.replace(Routing.generate('log'));
}, 3000);
};


I am using Symfony 3.4 and JQuery version is 2.1 and for the redirect I use the FOS JS Routing Bundle v~1.4










share|improve this question




















  • 1





    That's not possible, if your page redirects, then your upload will be canceled, unless you are doing client side routing.

    – Iwan Wijaya
    Nov 19 '18 at 13:26











  • @IwanWijaya Yep, the redirection is client side. My question is, isn't it possible to just redirect to this route, while the upload goes on? why does it wait for the 'save' route to finish?

    – Kiriakos Papachristou
    Nov 19 '18 at 13:33













  • Simple answer is you can't replace the window the request is being made in without aborting the request. Client side routing is completely different ...think "single page app".

    – charlietfl
    Nov 19 '18 at 13:46













  • You could accept the data and let background process run on server processing the data update sesssion with progreess details. Then initial request would be done very quickly and you can poll another script for progress updates

    – charlietfl
    Nov 19 '18 at 13:49













  • Ok, so in any case the save POST request should be finished on the client side for the redirection to happen, just on the same time I can create a process on the server that the client doesn't care about

    – Kiriakos Papachristou
    Nov 19 '18 at 13:54














0












0








0








I face a weird issue while redirecting after an asynchronous ajax POST request. I want to send an asynchronous POST request (file upload) to the server and until I wait for it to finish, since it takes some time, I want to redirect to a different route and there I can watch the status of the upload. My problem is that even if I state that the request is async it first waits for this request to finish, synchronously and then redirects me.
Am I missing anything by chance?



upload.save = function (data) {
var self = this,
result = self.uploadType.getSummaryData();

$.ajax({
method: 'POST',
url: Routing.generate('save'),
data: JSON.stringify({
//My data
}),
cache: false,
contentType : 'application/json',
dataType: 'json',
context: self
});

// Redirection
setTimeout(function () {
window.location.replace(Routing.generate('log'));
}, 3000);
};


I am using Symfony 3.4 and JQuery version is 2.1 and for the redirect I use the FOS JS Routing Bundle v~1.4










share|improve this question
















I face a weird issue while redirecting after an asynchronous ajax POST request. I want to send an asynchronous POST request (file upload) to the server and until I wait for it to finish, since it takes some time, I want to redirect to a different route and there I can watch the status of the upload. My problem is that even if I state that the request is async it first waits for this request to finish, synchronously and then redirects me.
Am I missing anything by chance?



upload.save = function (data) {
var self = this,
result = self.uploadType.getSummaryData();

$.ajax({
method: 'POST',
url: Routing.generate('save'),
data: JSON.stringify({
//My data
}),
cache: false,
contentType : 'application/json',
dataType: 'json',
context: self
});

// Redirection
setTimeout(function () {
window.location.replace(Routing.generate('log'));
}, 3000);
};


I am using Symfony 3.4 and JQuery version is 2.1 and for the redirect I use the FOS JS Routing Bundle v~1.4







php jquery ajax symfony fosjsroutingbundle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 12:52







Kiriakos Papachristou

















asked Nov 19 '18 at 13:19









Kiriakos PapachristouKiriakos Papachristou

3310




3310








  • 1





    That's not possible, if your page redirects, then your upload will be canceled, unless you are doing client side routing.

    – Iwan Wijaya
    Nov 19 '18 at 13:26











  • @IwanWijaya Yep, the redirection is client side. My question is, isn't it possible to just redirect to this route, while the upload goes on? why does it wait for the 'save' route to finish?

    – Kiriakos Papachristou
    Nov 19 '18 at 13:33













  • Simple answer is you can't replace the window the request is being made in without aborting the request. Client side routing is completely different ...think "single page app".

    – charlietfl
    Nov 19 '18 at 13:46













  • You could accept the data and let background process run on server processing the data update sesssion with progreess details. Then initial request would be done very quickly and you can poll another script for progress updates

    – charlietfl
    Nov 19 '18 at 13:49













  • Ok, so in any case the save POST request should be finished on the client side for the redirection to happen, just on the same time I can create a process on the server that the client doesn't care about

    – Kiriakos Papachristou
    Nov 19 '18 at 13:54














  • 1





    That's not possible, if your page redirects, then your upload will be canceled, unless you are doing client side routing.

    – Iwan Wijaya
    Nov 19 '18 at 13:26











  • @IwanWijaya Yep, the redirection is client side. My question is, isn't it possible to just redirect to this route, while the upload goes on? why does it wait for the 'save' route to finish?

    – Kiriakos Papachristou
    Nov 19 '18 at 13:33













  • Simple answer is you can't replace the window the request is being made in without aborting the request. Client side routing is completely different ...think "single page app".

    – charlietfl
    Nov 19 '18 at 13:46













  • You could accept the data and let background process run on server processing the data update sesssion with progreess details. Then initial request would be done very quickly and you can poll another script for progress updates

    – charlietfl
    Nov 19 '18 at 13:49













  • Ok, so in any case the save POST request should be finished on the client side for the redirection to happen, just on the same time I can create a process on the server that the client doesn't care about

    – Kiriakos Papachristou
    Nov 19 '18 at 13:54








1




1





That's not possible, if your page redirects, then your upload will be canceled, unless you are doing client side routing.

– Iwan Wijaya
Nov 19 '18 at 13:26





That's not possible, if your page redirects, then your upload will be canceled, unless you are doing client side routing.

– Iwan Wijaya
Nov 19 '18 at 13:26













@IwanWijaya Yep, the redirection is client side. My question is, isn't it possible to just redirect to this route, while the upload goes on? why does it wait for the 'save' route to finish?

– Kiriakos Papachristou
Nov 19 '18 at 13:33







@IwanWijaya Yep, the redirection is client side. My question is, isn't it possible to just redirect to this route, while the upload goes on? why does it wait for the 'save' route to finish?

– Kiriakos Papachristou
Nov 19 '18 at 13:33















Simple answer is you can't replace the window the request is being made in without aborting the request. Client side routing is completely different ...think "single page app".

– charlietfl
Nov 19 '18 at 13:46







Simple answer is you can't replace the window the request is being made in without aborting the request. Client side routing is completely different ...think "single page app".

– charlietfl
Nov 19 '18 at 13:46















You could accept the data and let background process run on server processing the data update sesssion with progreess details. Then initial request would be done very quickly and you can poll another script for progress updates

– charlietfl
Nov 19 '18 at 13:49







You could accept the data and let background process run on server processing the data update sesssion with progreess details. Then initial request would be done very quickly and you can poll another script for progress updates

– charlietfl
Nov 19 '18 at 13:49















Ok, so in any case the save POST request should be finished on the client side for the redirection to happen, just on the same time I can create a process on the server that the client doesn't care about

– Kiriakos Papachristou
Nov 19 '18 at 13:54





Ok, so in any case the save POST request should be finished on the client side for the redirection to happen, just on the same time I can create a process on the server that the client doesn't care about

– Kiriakos Papachristou
Nov 19 '18 at 13:54












2 Answers
2






active

oldest

votes


















0














So the issue seems to be on the server side. It seems like PHP needs to end the current session and store the session data with session_write_close() so that each ajax request doesn't have to wait for the next one if async is true.



So this is needed to allow concurrent request from the client side






share|improve this answer































    -1














    When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.



     $.ajax({
    method: 'POST',
    url: Routing.generate('save'),
    data: JSON.stringify({
    //My data
    }),
    cache: false,
    contentType : 'application/json',
    dataType: 'json',
    context: self,
    async: false
    });


    When async setting is set to true, a Asynchronous call is made instead of an Synchronous call.






    share|improve this answer
























    • Thanks, I am aware of that from the documentation. In my case I am looking for an async approach, so even save is not finished, do a client redirection to log

      – Kiriakos Papachristou
      Nov 19 '18 at 13:35











    • async: false is a horrible practice and should never ever be used. It is also deprecated by browser vendors and displays console warnings when you do try to use it. Don't do this!

      – charlietfl
      Nov 19 '18 at 13:38











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375522%2fjquery-ajax-asynchronous-redirection%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    So the issue seems to be on the server side. It seems like PHP needs to end the current session and store the session data with session_write_close() so that each ajax request doesn't have to wait for the next one if async is true.



    So this is needed to allow concurrent request from the client side






    share|improve this answer




























      0














      So the issue seems to be on the server side. It seems like PHP needs to end the current session and store the session data with session_write_close() so that each ajax request doesn't have to wait for the next one if async is true.



      So this is needed to allow concurrent request from the client side






      share|improve this answer


























        0












        0








        0







        So the issue seems to be on the server side. It seems like PHP needs to end the current session and store the session data with session_write_close() so that each ajax request doesn't have to wait for the next one if async is true.



        So this is needed to allow concurrent request from the client side






        share|improve this answer













        So the issue seems to be on the server side. It seems like PHP needs to end the current session and store the session data with session_write_close() so that each ajax request doesn't have to wait for the next one if async is true.



        So this is needed to allow concurrent request from the client side







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 12:52









        Kiriakos PapachristouKiriakos Papachristou

        3310




        3310

























            -1














            When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.



             $.ajax({
            method: 'POST',
            url: Routing.generate('save'),
            data: JSON.stringify({
            //My data
            }),
            cache: false,
            contentType : 'application/json',
            dataType: 'json',
            context: self,
            async: false
            });


            When async setting is set to true, a Asynchronous call is made instead of an Synchronous call.






            share|improve this answer
























            • Thanks, I am aware of that from the documentation. In my case I am looking for an async approach, so even save is not finished, do a client redirection to log

              – Kiriakos Papachristou
              Nov 19 '18 at 13:35











            • async: false is a horrible practice and should never ever be used. It is also deprecated by browser vendors and displays console warnings when you do try to use it. Don't do this!

              – charlietfl
              Nov 19 '18 at 13:38
















            -1














            When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.



             $.ajax({
            method: 'POST',
            url: Routing.generate('save'),
            data: JSON.stringify({
            //My data
            }),
            cache: false,
            contentType : 'application/json',
            dataType: 'json',
            context: self,
            async: false
            });


            When async setting is set to true, a Asynchronous call is made instead of an Synchronous call.






            share|improve this answer
























            • Thanks, I am aware of that from the documentation. In my case I am looking for an async approach, so even save is not finished, do a client redirection to log

              – Kiriakos Papachristou
              Nov 19 '18 at 13:35











            • async: false is a horrible practice and should never ever be used. It is also deprecated by browser vendors and displays console warnings when you do try to use it. Don't do this!

              – charlietfl
              Nov 19 '18 at 13:38














            -1












            -1








            -1







            When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.



             $.ajax({
            method: 'POST',
            url: Routing.generate('save'),
            data: JSON.stringify({
            //My data
            }),
            cache: false,
            contentType : 'application/json',
            dataType: 'json',
            context: self,
            async: false
            });


            When async setting is set to true, a Asynchronous call is made instead of an Synchronous call.






            share|improve this answer













            When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.



             $.ajax({
            method: 'POST',
            url: Routing.generate('save'),
            data: JSON.stringify({
            //My data
            }),
            cache: false,
            contentType : 'application/json',
            dataType: 'json',
            context: self,
            async: false
            });


            When async setting is set to true, a Asynchronous call is made instead of an Synchronous call.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 13:27









            A.M. PatelA.M. Patel

            629




            629













            • Thanks, I am aware of that from the documentation. In my case I am looking for an async approach, so even save is not finished, do a client redirection to log

              – Kiriakos Papachristou
              Nov 19 '18 at 13:35











            • async: false is a horrible practice and should never ever be used. It is also deprecated by browser vendors and displays console warnings when you do try to use it. Don't do this!

              – charlietfl
              Nov 19 '18 at 13:38



















            • Thanks, I am aware of that from the documentation. In my case I am looking for an async approach, so even save is not finished, do a client redirection to log

              – Kiriakos Papachristou
              Nov 19 '18 at 13:35











            • async: false is a horrible practice and should never ever be used. It is also deprecated by browser vendors and displays console warnings when you do try to use it. Don't do this!

              – charlietfl
              Nov 19 '18 at 13:38

















            Thanks, I am aware of that from the documentation. In my case I am looking for an async approach, so even save is not finished, do a client redirection to log

            – Kiriakos Papachristou
            Nov 19 '18 at 13:35





            Thanks, I am aware of that from the documentation. In my case I am looking for an async approach, so even save is not finished, do a client redirection to log

            – Kiriakos Papachristou
            Nov 19 '18 at 13:35













            async: false is a horrible practice and should never ever be used. It is also deprecated by browser vendors and displays console warnings when you do try to use it. Don't do this!

            – charlietfl
            Nov 19 '18 at 13:38





            async: false is a horrible practice and should never ever be used. It is also deprecated by browser vendors and displays console warnings when you do try to use it. Don't do this!

            – charlietfl
            Nov 19 '18 at 13:38


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375522%2fjquery-ajax-asynchronous-redirection%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents