Suppose we sent a request to the server to delete a file, using ajax. After sending that request we close our browser window. Does this request will delete that file? If yes then how? And if no then how?
Scenario 1(bad internet):
AJAX: Hey backend, master has said to delete this.jpg file.
AJAX sending request over slow internet
laptop's lid closed; request not sent; file not deleted; THE END
Scenario 2(good internet):
AJAX: Hey backend, master has said to delete this.jpg file.
AJAX request sent to the backend
laptop's lid closed to prevent deletion
Backend: Oh, master told you that? How can I know that?
AJAX: Yes, becuase master has only allowed request from domain.com:7777 and I am coming from there.
Backend: Right I see that now, ok ... database delete this file please!
file deleted
AJAX: Thanks, let me just inform master on the front end.
master opens up the lid in the hope to recover deleted file...
Master: Hey AJAX you deleted that file?
AJAX: Yes master, response status is 200 on that.
Master: Oh shit! What the fu*k did I do!!!
Mark is correct.
Additionally, if you wish to test API endpoints without a browser then you can use getpostman.com
Also, these resources may help
gomakethings.com/ajax-and-apis-with-vanilla-javas…
developer.mozilla.org/en-US/docs/Web/Guide/AJAX
developer.mozilla.org/en-US/docs/Web/API
seguetech.com/ajax-technology
If the request's been sent, closing the browser won't be able to get it back. So it'll arrive at the server and do what it usually does, which might be deleting a file.
This makes sense, because how would the message be stopped? It might be traveling through optic fiber cables, and nothing is faster than the speed of light, so there's no way to catch up with it before it reaches the server.
Of course if there was any delay, the message may not be (totally) sent yet when you close the browser, in which case I guess it depends on the browser but it probably won't complete.
Dennis San Jose
Fullstack Developer
Once the request had been sent, there's no way to cancel it. Unless there was any delay in the code that will trigger those ajax request.