20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
of these properties:
- onload: callback(responseData) (default = output response to the
console). In the context of the callback, the options object is
"this", noting that this call may have amended the options object
with state other than what the caller provided.
- onerror: callback(XHR onload event | exception)
(default = event or exception to the console).
- method: 'POST' | 'GET' (default = 'GET'). CASE SENSITIVE!
- payload: anything acceptable by XHR2.send(ARG) (DOMString,
Document, FormData, Blob, File, ArrayBuffer), or a plain object or
array, either of which gets JSON.stringify()'d. If payload is set
then the method is automatically set to 'POST'. By default XHR2
|
|
|
>
>
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
of these properties:
- onload: callback(responseData) (default = output response to the
console). In the context of the callback, the options object is
"this", noting that this call may have amended the options object
with state other than what the caller provided.
- onerror: callback(XHR onload event | exception) (default = event
or exception to the console). Triggered if the request generates
any response other than HTTP 200. In the context of the callback,
the options object is "this".
- method: 'POST' | 'GET' (default = 'GET'). CASE SENSITIVE!
- payload: anything acceptable by XHR2.send(ARG) (DOMString,
Document, FormData, Blob, File, ArrayBuffer), or a plain object or
array, either of which gets JSON.stringify()'d. If payload is set
then the method is automatically set to 'POST'. By default XHR2
|
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
value of that single header. If it's an array, it's treated as a
list of headers to return, and the 2nd argument is a map of those
header values. When a map is passed on, all of its keys are
lower-cased. When a given header is requested and that header is
set multiple times, their values are (per the XHR docs)
concatenated together with ", " between them.
When an options object does not provide onload() or onerror()
handlers of its own, this function falls back to
fossil.fetch.onload() and fossil.fetch.onerror() as defaults. The
default implementations route the data through the dev console and
(for onerror()) through fossil.error(). Individual pages may
overwrite those members to provide default implementations suitable
for the page's use.
Returns this object, noting that the XHR request is asynchronous,
and still in transit (or has yet to be sent) when that happens.
*/
window.fossil.fetch = function f(uri,opt){
const F = fossil;
if(!f.onload){
|
>
>
>
>
>
>
>
>
>
|
|
>
|
|
|
>
|
|
|
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
|
value of that single header. If it's an array, it's treated as a
list of headers to return, and the 2nd argument is a map of those
header values. When a map is passed on, all of its keys are
lower-cased. When a given header is requested and that header is
set multiple times, their values are (per the XHR docs)
concatenated together with ", " between them.
- beforesend/aftersend: optional callbacks which are called without
arguments immediately before the request is submitted and
immediately after it is received, regardless of success or
error. In the context of the callback, the options object is the
"this". These can be used to, e.g., keep track of in-flight
requests and update the UI accordingly, e.g. disabling/enabling DOM
elements. Any exceptions triggered by beforesend/aftersend are
caught and silently ignored.
When an options object does not provide
onload/onerror/beforesend/aftersend handlers of its own, this
function falls to defaults which are member properties of this
function with the same name, e.g. fossil.fetch.onload(). The
default onload/onerror implementations route the data through the
dev console and (for onerror()) through fossil.error(). The default
beforesend/aftersend are no-ops. Individual pages may overwrite
those members to provide default implementations suitable for the
page's use, e.g. keeping track of how many in-flight
Returns this object, noting that the XHR request is asynchronous,
and still in transit (or has yet to be sent) when that happens.
*/
window.fossil.fetch = function f(uri,opt){
const F = fossil;
if(!f.onload){
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
};
}
if('/'===uri[0]) uri = uri.substr(1);
if(!opt) opt = {};
else if('function'===typeof opt) opt={onload:opt};
if(!opt.onload) opt.onload = f.onload;
if(!opt.onerror) opt.onerror = f.onerror;
let payload = opt.payload, jsonResponse = false;
if(undefined!==payload){
opt.method = 'POST';
if(!(payload instanceof FormData)
&& !(payload instanceof Document)
&& !(payload instanceof Blob)
&& !(payload instanceof File)
|
>
>
|
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
};
}
if('/'===uri[0]) uri = uri.substr(1);
if(!opt) opt = {};
else if('function'===typeof opt) opt={onload:opt};
if(!opt.onload) opt.onload = f.onload;
if(!opt.onerror) opt.onerror = f.onerror;
if(!opt.beforesend) opt.beforesend = f.beforesend;
if(!opt.aftersend) opt.aftersend = f.aftersend;
let payload = opt.payload, jsonResponse = false;
if(undefined!==payload){
opt.method = 'POST';
if(!(payload instanceof FormData)
&& !(payload instanceof Document)
&& !(payload instanceof Blob)
&& !(payload instanceof File)
|
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
the response. */
jsonResponse = true;
x.responseType = 'text';
}else{
x.responseType = opt.responseType||'text';
}
x.onload = function(e){
if(200!==this.status){
opt.onerror(e);
return;
}
const orh = opt.responseHeaders;
let head;
if(true===orh){
|
>
|
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
the response. */
jsonResponse = true;
x.responseType = 'text';
}else{
x.responseType = opt.responseType||'text';
}
x.onload = function(e){
try{opt.aftersend()}catch(e){/*ignore*/}
if(200!==this.status){
opt.onerror(e);
return;
}
const orh = opt.responseHeaders;
let head;
if(true===orh){
|
174
175
176
177
178
179
180
181
182
183
184
|
? JSON.parse(this.response) : this.response];
if(head) args.push(head);
opt.onload.apply(opt, args);
}catch(e){
opt.onerror(e);
}
};
if(undefined!==payload) x.send(payload);
else x.send();
return this;
};
|
>
>
>
>
|
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
? JSON.parse(this.response) : this.response];
if(head) args.push(head);
opt.onload.apply(opt, args);
}catch(e){
opt.onerror(e);
}
};
try{opt.beforesend()}catch(e){/*ignore*/}
if(undefined!==payload) x.send(payload);
else x.send();
return this;
};
window.fossil.fetch.beforesend = function(){};
window.fossil.fetch.aftersend = function(){};
|