-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonp.js
More file actions
33 lines (28 loc) · 926 Bytes
/
jsonp.js
File metadata and controls
33 lines (28 loc) · 926 Bytes
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
define(['./io'], function (io) {
'use strict';
// JSONP handler
// This is a browser-only module.
var counter = 0;
function jsonpTransport (options, prep) {
var callback = options.callback || 'callback',
name = '__io_jsonp_callback_' + (counter++),
script = document.createElement('script'),
deferred = new io.Deferred();
window[name] = function (value) {
delete window[name];
script.parentNode.removeChild(script);
deferred.resolve(value);
};
script.onerror = function (e) {
delete window[name];
script.parentNode.removeChild(script);
deferred.reject(new io.FailedIO(null, options, e));
};
script.src = prep.url + (prep.url.indexOf('?') >= 0 ? '&' : '?') +
'callback=' + encodeURIComponent(name);
document.documentElement.appendChild(script);
return deferred.promise || deferred;
}
io.transports.jsonp = jsonpTransport;
return io.makeVerb('jsonp', 'transport');
});