It is possible to configure the json type information before entering the event, but it does not work for me, and I get errors constantly
function makeRequest6() {
var httpRequest = new XMLHttpRequest();
httpRequest.responseType = 'json';
httpRequest.onload = function()
{
//var json= JSON.parse(httpRequest.responseText); //i wnat avoid this line
var json=httpRequest.responseText; // Here it failed
console.log("Accepted");
console.log(json);
var latestUsd=json.rates.USD;
console.log(latestUsd);
};
httpRequest.open('GET','http://api.exchangeratesapi.io/v1/latest?access_key=XXXXXXXXXXXXXXXXXXXX');
httpRequest.send();
}
ERROR:
main19.JS:253 Uncaught DOMException: Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was 'json').
at XMLHttpRequest.httpRequest.onload (http://127.0.0.1:5500/lec_19_Ajax_%20getData_JSON/main19.JS:253:30)