From d993d45e90e8e4b057036f5403f84df804e15bc0 Mon Sep 17 00:00:00 2001 From: jlukic Date: Wed, 13 May 2015 18:24:53 -0400 Subject: [PATCH] Resolve XHR error conditions for abort, with CORS --- src/definitions/behaviors/api.js | 56 ++++++++++++++------------------ 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/definitions/behaviors/api.js b/src/definitions/behaviors/api.js index c8b57af16..7f31e56b6 100644 --- a/src/definitions/behaviors/api.js +++ b/src/definitions/behaviors/api.js @@ -353,11 +353,13 @@ $.api = $.fn.api = function(parameters) { ; // page triggers abort on navigation, dont show error setTimeout(function() { - if(status !== 'abort') { - module.request.rejectWith(context, [xhr, status, httpMessage]); + if(xhr.readyState !== undefined && xhr.readyState === 0) { + module.debug('Request Aborted (Most likely caused by page navigation or CORS Policy)', status, httpMessage); + settings.onAbort.call(context, status, $module); + module.reset(); } else { - module.reset(); + module.request.rejectWith(context, [xhr, status, httpMessage]); } }, timeLeft); } @@ -408,39 +410,32 @@ $.api = $.fn.api = function(parameters) { ; // let em know unless request aborted if(xhr !== undefined) { - // readyState 4 = done, anything less is not really sent - if(xhr.readyState !== undefined && xhr.readyState == 4) { - // if http status code returned and json returned error, look for it - if( xhr.status != 200 && httpMessage !== undefined && httpMessage !== '') { - module.error(error.statusMessage + httpMessage, ajaxSettings.url); - } - else { - if(status == 'error' && settings.dataType == 'json') { - try { - response = $.parseJSON(xhr.responseText); - if(response && response.error !== undefined) { - errorMessage = response.error; - } - } - catch(e) { - module.error(error.JSONParse); + // if http status code returned and json returned error, look for it + if( xhr.status != 200 && httpMessage !== undefined && httpMessage !== '') { + module.error(error.statusMessage + httpMessage, ajaxSettings.url); + } + else { + if(status == 'error' && settings.dataType == 'json') { + try { + response = $.parseJSON(xhr.responseText); + if(response && response.error !== undefined) { + errorMessage = response.error; } } + catch(e) { + module.error(error.JSONParse); + } } - module.remove.loading(); - module.set.error(); - // show error state only for duration specified in settings - if(settings.errorDuration) { - setTimeout(module.remove.error, settings.errorDuration); - } - module.debug('API Request error:', errorMessage); - settings.onError.call(context, errorMessage, $module); } - else { - settings.onAbort.call(context, errorMessage, $module); - module.debug('Request Aborted (Most likely caused by page change or CORS Policy)', status, httpMessage); + module.remove.loading(); + // show error state only for duration specified in settings + if(settings.errorDuration !== false) { + module.set.error(); + setTimeout(module.remove.error, settings.errorDuration); } + module.debug('API Request error:', errorMessage); + settings.onError.call(context, errorMessage, $module); } } } @@ -631,7 +626,6 @@ $.api = $.fn.api = function(parameters) { if( xhr && xhr.state() !== 'resolved') { module.debug('Cancelling API request'); xhr.abort(); - module.request.rejectWith(settings.apiSettings); } },