Es posible configurar la información de tipo json antes de entrar al evento, pero no me funciona, y me salen errores constantemente
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)