-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ajax.js
99 lines (86 loc) · 2.92 KB
/
ajax.js
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
import { config } from './config.js';
import { logMessage, logError, parseUrl, buildUrl, _each } from './utils.js';
const XHR_DONE = 4;
/**
* Simple IE9+ and cross-browser ajax request function
* Note: x-domain requests in IE9 do not support the use of cookies
*
* @param url string url
* @param callback {object | function} callback
* @param data mixed data
* @param options object
*/
export const ajax = ajaxBuilder();
export function ajaxBuilder(timeout = 3000, {request, done} = {}) {
return function(url, callback, data, options = {}) {
try {
let x;
let method = options.method || (data ? 'POST' : 'GET');
let parser = document.createElement('a');
parser.href = url;
let callbacks = typeof callback === 'object' && callback !== null ? callback : {
success: function() {
logMessage('xhr success');
},
error: function(e) {
logError('xhr error', null, e);
}
};
if (typeof callback === 'function') {
callbacks.success = callback;
}
x = new window.XMLHttpRequest();
x.onreadystatechange = function () {
if (x.readyState === XHR_DONE) {
if (typeof done === 'function') {
done(parser.origin);
}
let status = x.status;
if ((status >= 200 && status < 300) || status === 304) {
callbacks.success(x.responseText, x);
} else {
callbacks.error(x.statusText, x);
}
}
};
// Disabled timeout temporarily to avoid xhr failed requests. https://github.com/prebid/Prebid.js/issues/2648
if (!config.getConfig('disableAjaxTimeout')) {
x.ontimeout = function () {
logError(' xhr timeout after ', x.timeout, 'ms');
};
}
if (method === 'GET' && data) {
let urlInfo = parseUrl(url, options);
Object.assign(urlInfo.search, data);
url = buildUrl(urlInfo);
}
x.open(method, url, true);
// IE needs timeout to be set after open - see #1410
// Disabled timeout temporarily to avoid xhr failed requests. https://github.com/prebid/Prebid.js/issues/2648
if (!config.getConfig('disableAjaxTimeout')) {
x.timeout = timeout;
}
if (options.withCredentials) {
x.withCredentials = true;
}
_each(options.customHeaders, (value, header) => {
x.setRequestHeader(header, value);
});
if (options.preflight) {
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
}
x.setRequestHeader('Content-Type', options.contentType || 'text/plain');
if (typeof request === 'function') {
request(parser.origin);
}
if (method === 'POST' && data) {
x.send(data);
} else {
x.send();
}
} catch (error) {
logError('xhr construction', error);
typeof callback === 'object' && callback !== null && callback.error(error);
}
}
}