I am trying to get data from AWS JSON. It is not on my domain and I can't get the data. The error that chrome gives is below Could anyone review my code and give me some tips?
Second, I was using Insomnia just using the link and was able to get the specific data point that I wanted using $.0.Ed0320
. How do I translate this into JavaScript.
I have tried using a xmlHttpRequest.
Here is the JSON:
[
{
"Ed0320": "8.010886",
"TmStamp": "2019-08-07 15:15:00",
"Ed0340": "21.15973",
"Ed0305": "0.2966875",
"Ed0313": "3.344086"
},
{
"Ed0320": "6.761719",
"TmStamp": "2019-08-07 15:10:00",
"Ed0340": "17.47292",
"Ed0305": "0.2349026",
"Ed0313": "2.789934"
}
]
Here is my XML:
function reqListener() {
// parse the the data
var data = JSON.parse(this.responseText)
// create an array from the required data (result.Stats -> watch the capital 'S')
var mappedData = data[0];
// display data
document.getElementById('demo').innerHTML = mappedData.join()
}
function loadData(url) {
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", url);
oReq.send();
}
Chrome gives the error of Access to XMLHttpRequest at <fileName> from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
function calcUVI(Ed0305, Ed0313, Ed0320, Ed0340){
total305=(Ed0305*0.8058)
total313=(Ed0313*0.0887)
total320=(Ed0320*0.0324)
total340=(Ed0340*0.0131)
UVI = total305 + total313 + total320 + total340
return UVI
}
Also, I would like to change the url based on this function below. Is there a way to input this into the fetch?
function changeText(){
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
var day = d.getDate();
if (month <= 9){
month = '0'+month;
}
if (day <= 9){
day = '0'+day;
}
var dateTotal = year + month + day;
url = "https://cors-escape.herokuapp.com/https://tepfsail50.execute-api.us-west-2.amazonaws.com/v1/report/metguv?rptdate=" + dateTotal;
console.log(url);
}
Thank you in advance for your help. I am really new to all of this.