Error handling from JS remoting

We are using JS remoting. Found this interesting race condition:

  1. JS Remoting AJAX request made by user.
  2. Before it completes the user moves to another page
  3. When user moves browser to another page the js remoting ajax request is aborted.
  4. The abort invokes the error handler and we have a generic error handler which pops up a dialog showing exception info.
  5. In some cases, the dialog will flash up for a second because the page navigation away hasn’t completed.

This looks ugly and we would prefer if the error handler knew there was a page unload happening and not show the error dialog if this was happening.

So, I tried adding a page unload listener and setting a flag in that which can be subsequently checked.

jQuery(window).unload(function() {
    unloadhappening = true;
});

This approach will work in most ajax designs but won’t with JS remoting. JS remoting, calls the error handler before the unload event. Not sure why?

I tried the earlier beforeunload event:

jQuery(window).on('beforeunload', function() {
    unloadhappening = true;
    return '';  
}); 

This won’t work because this event just shows up another dialog.

So I tried to add a listener to all anchor tags that have a http(s) hyperlink

jQuery('a[href^=http]').click( function() {
    unloadhappening = true; 
})

This won’t work for relative URLs.

So I tried to adding a listener to all anchor tags that have a href

jQuery('a[href]').click( function() {
    unloadhappening = true; 
})

This will filter out components that won’t work because it won’t filter out components that don’t trigger a page unload such as:

   <a href="#adviceTab">Advice</a>

So before I go and write a selector to filter out anything with a fragment identifier, there are also some anchors that are coming form JQuery UI that do:

   <a href="javascript:%20void%280%29%3B" class="calToday"    onclick="DatePicker.datePicker.selectDate('today');return false;">Today</a>

I am starting to think this is a more of a hack that will never work as opposed to a solution. Do you have a good idea for this problem?

The only idea I can think of is to check the exception, error, event info returned by
JS remoting for an aborted request. I see: Strings:

Unable to connect with Server 

and

Communication Failure

But that error message is also used when a JS remoting ajax request is aborted because of network failure in the middle of an ajax request – not just because it is aborted because of navigation to another page. If you want different error handling for the two it is impossible to differentiate between them.

Note I have to use JS Remoting here. Using forceTK etc is not an option. So I am wondering has anyone else seen this and if so what is your solution?

Thanks

Answer

Recently I have found out one problem in JS remoting in salesforce is that It doesnot work with the Enter key. For eg. If you hit any key apart from enter My JS script is calling. On every Event it is working but when I it enter it does not work. Then to verify the same I have put if statements to catch the key value == 13(Enter key Ascii code) to verify if it is Enter key issue only.

TO my observation I am correct the issue persists with enter keyevent. Normally JS remoting will listen until the response is returned from the apex class. But on hitting enter key it escapes every thing and just executes the complete code and exit. It will not at all listen for the response. Infact I have seen in the logs with enter key it is skipping the JS remoting call.

Please check if same is the case with your event your are trying.

Attribution
Source : Link , Question Author : More Than Five , Answer Author : Mukul Goel

Leave a Comment