/
loadJson.js
73 lines (67 loc) · 2.6 KB
/
loadJson.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
define([
'./clone',
'./defined',
'./DeveloperError',
'./loadText'
], function(
clone,
defined,
DeveloperError,
loadText) {
'use strict';
var defaultHeaders = {
Accept : 'application/json,*/*;q=0.01'
};
// note: */* below is */* but that ends the comment block early
/**
* Asynchronously loads the given URL as JSON. Returns a promise that will resolve to
* a JSON object once loaded, or reject if the URL failed to load. The data is loaded
* using XMLHttpRequest, which means that in order to make requests to another origin,
* the server must have Cross-Origin Resource Sharing (CORS) headers enabled. This function
* adds 'Accept: application/json,*/*;q=0.01' to the request headers, if not
* already specified.
*
* @exports loadJson
*
* @param {String} url The URL to request.
* @param {Object} [headers] HTTP headers to send with the request.
* 'Accept: application/json,*/*;q=0.01' is added to the request headers automatically
* if not specified.
* @param {Request} [request] The request object. Intended for internal use only.
* @returns {Promise.<Object>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
*
*
* @example
* Cesium.loadJson('http://someUrl.com/someJson.txt').then(function(jsonData) {
* // Do something with the JSON object
* }).otherwise(function(error) {
* // an error occurred
* });
*
* @see loadText
* @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
* @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
*/
function loadJson(url, headers, request) {
//>>includeStart('debug', pragmas.debug);
if (!defined(url)) {
throw new DeveloperError('url is required.');
}
//>>includeEnd('debug');
if (!defined(headers)) {
headers = defaultHeaders;
} else if (!defined(headers.Accept)) {
// clone before adding the Accept header
headers = clone(headers);
headers.Accept = defaultHeaders.Accept;
}
var textPromise = loadText(url, headers, request);
if (!defined(textPromise)) {
return undefined;
}
return textPromise.then(function(value) {
return JSON.parse(value);
});
}
return loadJson;
});