Javascript try-catch does not break 'Failed to load resource: net :: ERR_CONNECTION_RESET'

I have a multi-tasking file downloader, but sometimes 1 out of 10 files doesn’t execute when loading, and it returns Failed to load resource: net::ERR_CONNECTION_RESET in the chrome console. I tried to catch it with try-catch, but it acts as if an error had not occurred. What am I doing wrong?

 var ajax = new XMLHttpRequest(); ajax.open("POST", "/multiFileUploadHandler.php"); try { ajax.send(formdata); } catch (err) { alert('Error: '+err); } 
+7
javascript try-catch
source share
1 answer

This is probably asynchronous. Try to catch this with the onerror event handler.

 ajax.onerror = function(error) { // handle error }; 

edit: fixed syntax.

+3
source share

All Articles