Skip to content Skip to sidebar Skip to footer

Possible To Make Ajax Call That Doesn't Return?

Is it possible to send an jQuery.ajax call or equivalent without any sort of response? I want to trigger something on the server as I leave the page with the onbeforeunload comman

Solution 1:

Every request has a response. Even if the server throws an error a response is coming back with the error.

You can ignore the response if you like to just don't add a success callback.

$.ajax({
    url: "theURL",
    data: theData
});

Solution 2:

You could use the navigator sendBeacon API mentioned referenced in this stack overflow answer or directly linked to here.

From the description on the site, it asynchronously sends off some data and just checks if it was able to queue the data for sending or not. The unload handler shouldn't ignore the request like it might do with other asynchronous XMLHttpRequests.

navigator.sendBeacon(url, data) - The sendBeacon() method returns true if the user agent is able to successfully queue the data for transfer, Otherwise it returns false.

...

ensuring that the data has been sent during the unloading of a document is something that has traditionally been difficult for developers, because user agents typically ignore asynchronous XMLHttpRequests made in an unload handler.

Solution 3:

Assuming you're using something like this:

$.ajax({
   url: "test.html",
   context: document.body
})

(borrowed from http://api.jquery.com/jQuery.ajax/ and edited), you can discard the response.

Solution 4:

If you are trying to return a response as soon as possible I think that is more of a server architecture decision. You can offload the actual work the request involves to a broker or something so you can return to the user as soon as possible? I don't think sending just a one way message is feasible

Post a Comment for "Possible To Make Ajax Call That Doesn't Return?"