/
jsonptest.html
116 lines (92 loc) · 3.46 KB
/
jsonptest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
// Default data loader
// It does more than we need
//=========================================================
// ajax(url,{'complete':function,'error':function,'dataType':'json'})
// complete: function - a function executed on completion
// error: function - a function executed on an error
// cache: break the cache
// dataType: json - will convert the text to JSON
// jsonp - will add a callback function and convert the results to JSON
function ajax(url,attrs){
if(typeof url!=="string") return false;
if(!attrs) attrs = {};
var cb = "",qs = "";
var oReq;
if(attrs['dataType']=="jsonp"){
if(typeof attrs['callback']==="string") cb = attrs['callback'];
else cb = 'fn_'+(new Date()).getTime();
window[cb] = function(rsp){
if(typeof attrs.success==="function") attrs.success.call((attrs['this'] ? attrs['this'] : this), rsp, attrs);
};
}
if(typeof attrs.cache==="boolean" && !attrs.cache) qs += (qs ? '&':'')+(new Date()).valueOf();
if(cb) qs += (qs ? '&':'')+'callback='+cb;
if(attrs.data) qs += (qs ? '&':'')+attrs.data;
// Build the URL to query
attrs['url'] = url+(qs ? '?'+qs:'');
if(attrs['dataType']=="jsonp"){
var script = document.createElement('script');
script.src = attrs['url'];
document.body.appendChild(script);
return this;
}
// code for IE7+/Firefox/Chrome/Opera/Safari or for IE6/IE5
oReq = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
oReq.addEventListener("load", window[cb] || complete);
oReq.addEventListener("error", error);
if(attrs.beforeSend) oReq = attrs.beforeSend.call((attrs['this'] ? attrs['this'] : this), oReq, attrs);
function complete(evt) {
if(oReq.status === 200) {
attrs.header = oReq.getAllResponseHeaders();
var rsp = oReq.response || oReq.responseText;
// Parse out content in the appropriate callback
if(attrs['dataType']=="json") try { rsp = JSON.parse(rsp.replace(/[\n\r]/g,"\\n").replace(/^([^\(]+)\((.*)\)([^\)]*)$/,function(e,a,b,c){ return (a==cb) ? b:''; }).replace(/\\n/g,"\n")) } catch(e){};
if(attrs['dataType']=="script"){
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.innerHTML = rsp;
document.head.appendChild(fileref);
}
attrs['statusText'] = 'success';
if(typeof attrs.success==="function") attrs.success.call((attrs['this'] ? attrs['this'] : this), rsp, attrs);
}else{
attrs['statusText'] = 'error';
error(evt);
}
if(typeof attrs.complete==="function") attrs.complete.call((attrs['this'] ? attrs['this'] : this), rsp, attrs);
}
function error(evt){
if(typeof attrs.error==="function") attrs.error.call((attrs['this'] ? attrs['this'] : this),evt,attrs);
}
if(attrs['dataType']) oReq.responseType = attrs['dataType'];
try{ oReq.open('GET', attrs['url']); }
catch(err){ error(err); }
try{ oReq.send(); }
catch(err){ error(err); }
return this;
} // End default ajax() function
console.log('testing');
ajax("http://astrog80.astro.cf.ac.uk/GW/gwosc_gracedb.jsonp",{
"dataType": "jsonp",
"this": this,
"callback":"catdata",
"error": function(error,attr) {
this.log('events error:',error,attr);
//alert("Fatal error loading input file: '"+attr.url+"'. Sorry!");
},
"success": function(dataIn,attr){
console.log('jsonp read',dataIn);
//parseData(dataIn,attr,this);
}
});
</script>
</body>
</html>
smile
yes