skip to Main Content

I want to open a window, make an ajax call and close the window.
I want to do it as fast as possible and i was wondering if i have to wait for the ajax response before closing the window?

Currently i’m doing it like this:

        $.ajax({
            url: requestURL,
            type: 'GET',
            dataType: "json",
            cache: false,
            timeout: 30000,
            always: function () {
                closeWindow();
            }
        });

However, i was wondering if the ajax will reach the server 100% on all browsers if i will do it like this:

        $.ajax({
            url: requestURL,
            type: 'GET',
            dataType: "json",
            cache: false,
            timeout: 30000,
            always: function () {

            }
        });
        closeWindow();

//THIS HAS CONFIRMED TO NOT WORK AND MISS OUT SOME REQUESTS

closeWindow() implementation is irrelevant.

The full usecase is as follows:

  1. I send a user a link on Whatsapp/Telegram/Messenger
  2. User clicks the link
  3. Browser is open -> issue an ajax call -> closing the window.

EDIT
To clarify, i don’t care what was the server response for the call. I just want to make sure that the browser issued the HTTP GET to the server and then close the window.

EDIT 2
AJAX is not a must, also, better to use vanilla JS

5

Answers


  1. Chosen as BEST ANSWER

    I guess the answer was at the back of my head: use an Image object to send the request and close the window right after:

            var img = new Image();
            img.src =  urlWithParamters;
            img.onload = function () {
                closeWindow();
            };
            img.onerror = function () {
                closeWindow();
            };
    
    //safety:
            setTimeout(closeWindow,5000);
    

  2. Since you are using JQuery, you can use the “done” callback option, and set your function inside so the window will close if the request was successful.

    $.ajax({
       url: requestURL,
       type: 'GET',
       dataType: "json",
       cache: false
    }).done(function() {
       closeWindow();
    });
    

    You can also use the “fail” callback option to manage potential errors.

    Login or Signup to reply.
  3. You can do it like you said, send the ajax request and close the window, since it’s async methods, it will make the call, if you wanna be absolutely SURE, write a little PHP script that writes something in a file with a little sleep at the top to check that it made the call, but it will do it.

    Edit : or use this method and close the browser in the done function, it will take 1ms or so

    Login or Signup to reply.
  4. following function can close your tab after 1 sec, without waiting for the ajax response

        $.ajax({
            url: requestURL,
            type: 'GET',
            dataType: "json",
            cache: false,
        });
        setInterval(function(){ 
            close();
        }, 1000);
    
    Login or Signup to reply.
  5. I believe that if you want to know when your request was launched you could use .ajaxStart as explained here.

    $(document).ajaxStart(function() {
       console.log('Ajax call started');
       closeWindow();
    });
    

    Or you could try some raw js like the solution explained here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search