forked from rom3r4/Karma-Read-JSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarma-json-reader.js
36 lines (29 loc) · 1.01 KB
/
karma-json-reader.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
(function(window, document, undefined) {
'use strict';
window.KarmaJSONReader = function(baseUrl) {
var baseUrl = baseUrl || 'test/data/';
this.setBaseUrl = function(_baseUrl_) {
baseUrl = _baseUrl_;
};
this.readJSON = function(filename) {
var xhr = new XMLHttpRequest();
var json = null;
var url = baseUrl + filename;
xhr.open('GET', url, false);
xhr.onload = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
json = JSON.parse(xhr.responseText);
} else {
console.error('readJSON - ', url, xhr.statusText);
}
}
};
xhr.onerror = function() {
console.error('readJSON - ', url, xhr.statusText);
};
xhr.send(null);
return json;
};
};
})(window, document);