Possible to make AJAX call that doesn't return?

前端 未结 4 1637
忘掉有多难
忘掉有多难 2020-12-30 11:57

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

4条回答
  •  长发绾君心
    2020-12-30 12:45

    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.

提交回复
热议问题