简体   繁体   中英

JQuery Ajax Asynchronous Redirection

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM