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.
This commit is contained in:
hallgren
2012-03-22 14:09:53 +00:00
parent 891ef8e08a
commit f899d54057
2 changed files with 57 additions and 29 deletions

View File

@@ -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 ----------------------------------------------------- */