terriajs
Version:
Geospatial data visualization platform.
50 lines (42 loc) • 1.18 kB
text/typescript
import Resource from "terriajs-cesium/Source/Core/Resource";
export default function loadJson(
urlOrResource: any,
headers?: any,
body?: any,
asForm: boolean = false
): Promise<any> {
let responseType: XMLHttpRequestResponseType = "json";
let jsonPromise: Promise<any>;
let params: any = {
url: urlOrResource,
headers: headers
};
if (body !== undefined) {
// We need to send a POST
params.headers = headers ?? {};
params.headers["Content-Type"] = "application/json";
if (asForm) {
const data = new FormData();
Object.entries(body).forEach(([key, value]) =>
data.append(key, JSON.stringify(value))
);
params.data = data;
} else {
params.data = JSON.stringify(body);
}
params.responseType = responseType;
jsonPromise =
urlOrResource instanceof Resource
? urlOrResource.post(body, {
responseType: responseType
})!
: Resource.post(params)!;
} else {
// Make a GET instead
jsonPromise =
urlOrResource instanceof Resource
? urlOrResource.fetchJson()!
: Resource.fetchJson(params)!;
}
return jsonPromise;
}