2

On close of a browser tab, I'm attempting to do an automatic save of the user's changes. I have verified this works at other points in the timeline; the same function is used. I call it in the unload handler:

$( window ).unload(function() {        
    saveAnnotations(pdfState.pdfs[pdfState.activePdfIndex].PdfPages[pdfState.activePageIndex]);
});

Saving the user's changes involves a RESTful PUT inside that saveAnnotations function, using the Oboe library:

oboe({
  url: configuration.rootApiUrl + 'PdfPages/' + page.ID,
  method: 'PUT',
  body: page
}).done(function(jobs) { // Our call does not provide a callback
  if(callback !== undefined && _.isFunction(callback)) {
    callback();
  }
});

The server receiving this is a Microsoft Web API server. I'm running both the client and the server locally for testing.

In diagnosing this I edited the above code in place to be a GET rather than a PUT, and it hit a breakpoint inside the route on the server. However, when doing a PUT, I do not hit the breakpoint inside that server route on tab close. Why would it work for a GET and not a PUT? What must I do so that the server can receive my PUT? My hunch is that it is client side, not server side. If it would be helpful to see the server route I can post that, but otherwise I don't want to clutter the question.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Scotty H
  • 6,432
  • 6
  • 41
  • 94
  • the unload event is different from other events. It is meant to prompt the user with a message, not fire an ajax call. But you can try a few things I guess. Oh look, here is a link that was the first result when typing your question into google. http://stackoverflow.com/questions/3584288/can-the-unload-event-be-used-to-reliably-fire-ajax-request – QBM5 Feb 25 '16 at 16:53
  • @QBM5 What is the correct event to listen on then? The linked question doesn't seem to provide that information. – Scotty H Feb 25 '16 at 17:01
  • 1
    You can **never** do this reliably. Better to just autosave every X minutes, or every X changes that a user has done. – Stephen Feb 25 '16 at 17:28
  • As I said. When the user closer the tab that's it. There is no way to reliably send an Ajax call on any event related to a close – QBM5 Feb 25 '16 at 17:40

0 Answers0