This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
dwelibs/src/file.js

37 lines
826 B
JavaScript

DWE.File = {}
DWE.File.httpGET = function (url) {
return new Promise(function (resolve, reject) {
var xmlHttp = new XMLHttpRequest()
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
resolve(xmlHttp.responseText)
} else if (xmlHttp.readyState == 4 && xmlHttp.status >= 400) {
reject(xmlHttp.responseText)
}
}
xmlHttp.open('GET', url, true)
xmlHttp.responseType = 'text'
xmlHttp.send(null)
})
}
DWE.File.loadJson = function (url) {
return new Promise(function (resolve, reject) {
var promise = DWE.File.httpGET(url)
promise.then(function (data) {
var json
try {
json = JSON.parse(data)
} catch (e) {
return reject(e)
}
resolve(json)
}, reject)
})
}