Appreciate your time in reading this inquiry!
For the past few days, I've been grappling with an AJAX request issue. Despite scouring through numerous answers on Stack Overflow, I'm still unable to find a resolution. Any assistance would be greatly appreciated.
My objective is to execute a request using vanilla JavaScript's XMLHttpRequest
method to fetch data in JSON format from the Dark Sky API. Here is the URI I'm trying to send my request to (revealing my key isn't a concern) Click here for a sample API request
The root of the problem lies in encountering repeated CORS errors. While I have previously addressed this issue using JSONP requests, I am now exploring alternative routes to bypass using JSONP.
I experimented with headers, which did eliminate the CORS error in the console. However, upon inspection of the response, no data is transmitted after the request. Below is the header-less request that triggers a CORS error:
function CallAjax() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.darksky.net/forecast/166731d8eab28d33a26c5a51023eff4c/43.11201,-79.11857", true);
xhr.onload = function () {
console.log(xhr.responseText);
alert("Success");
};
xhr.send();
}
CallAjax();
Is there a possibility to make an API call that refrains from automatically responding with the header "Access-Control-Allow-Origin","http://localhost:9000/" or any other header that could circumvent the CORS error and include JSON data within the response?
Your guidance on this matter is deeply appreciated!