read json file

javascript - 2018-09-01 12:37:58
  allow to read a json file

<script>
	function readTextFile(file, callback) {
		var rawFile = new XMLHttpRequest();
		rawFile.overrideMimeType("application/json");
		rawFile.open("GET", file, true);
		rawFile.onreadystatechange = function () {
			if (rawFile.readyState === 4 && rawFile.status == "200") {
				callback(rawFile.responseText);
			}
		}
		rawFile.send(null);
	}

	//usage:
	readTextFile("datas.json", function (text) {
		var data = JSON.parse(text);
		console.log(data);
	});
</script>