1

I'm trying to make an ajax request to some logging service using jQuery, the request is initiated by clicking a link on the page, but sometimes I see in dev tools that the request gets the status "canceled".

  1. Is it because of a race condition? Clicking on the link unloads the page, and if the request-response cycle is not yet completed, it gets canceled?
  2. If so, what does it mean, canceled? I know I'm not getting a response, and I don't care about that (no valuable information is sent in that response anyway), but can I count on the request making it through to the server?

Thanks

1 Answers1

0

Yes, it will be canceled. If the user click link then link URL loading has starting. When the URL response first byte then all ajax request on current page will cancel immediately and will move to the URL.

If you want to ensure logs, you should use javascript to cancel default link click event, and logging, and move to the URL when logging has done.

<a href="http://example.com" id="link1">Some Link</a>

<script>
$('#link1').on('click', function (e) {
  e.preventDefault();
  $.get('/path/to/logging', function () {
    // move to the URL after logging
    location.href = this.href;
  });
});
</script>
tenshi
  • 56
  • 5