From f8ea4b8046b8ddd218694609532546f68641f628 Mon Sep 17 00:00:00 2001 From: hallgren Date: Thu, 22 Mar 2012 14:09:53 +0000 Subject: pgf_online.js: error callbacks To enable customized error handling, the methods in the pgf_online objects and the AJAX server call functions in support.js accept an error callback function as an extra argument, in addition to the callback (continuation) for normal results. --- src/www/minibar/support.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/www/minibar/support.js') diff --git a/src/www/minibar/support.js b/src/www/minibar/support.js index 08327d102..0e332a5c7 100644 --- a/src/www/minibar/support.js +++ b/src/www/minibar/support.js @@ -38,7 +38,7 @@ var json = {next:0}; // Like jsonp, but instead of passing the name of the callback function, you // pass the callback function directly, making it possible to use anonymous // functions. -function jsonpf(url,callback) +function jsonpf(url,callback,errorcallback) { var name="callback"+(json.next++); json[name]=function(x) { delete json[name]; callback(x); } @@ -71,10 +71,12 @@ function ajax_http(method,url,body,callback,errorcallback) { else alert(errortext) } else { - var statechange=function() { + function statechange() { if (http.readyState==4 || http.readyState=="complete") { if(http.status<300) callback(http.responseText,http.status); - else if(errorcallback) errorcallback(http.responseText,http.status); + else if(errorcallback) + errorcallback(http.responseText,http.status, + http.getResponseHeader("Content-Type")); else alert("Request for "+url+" failed: " +http.status+" "+http.statusText); } @@ -96,8 +98,8 @@ function ajax_http_post(url,formdata,callback,errorcallback) { } // JSON via AJAX -function ajax_http_get_json(url,cont) { - ajax_http_get(url,function(txt) { cont(eval("("+txt+")")); }); +function ajax_http_get_json(url,cont,errorcallback) { + ajax_http_get(url,function(txt){cont(eval("("+txt+")"));}, errorcallback); } function sameOrigin(url) { @@ -107,9 +109,9 @@ function sameOrigin(url) { } // Use AJAX when possible, fallback to JSONP -function http_get_json(url,cont) { - if(sameOrigin(url)) ajax_http_get_json(url,cont); - else jsonpf(url,cont); +function http_get_json(url,cont,errorcallback) { + if(sameOrigin(url)) ajax_http_get_json(url,cont,errorcallback); + else jsonpf(url,cont,errorcallback); } /* --- URL construction ----------------------------------------------------- */ -- cgit v1.2.3