Jump to content

so i am trying to fetch a URL but i get this error 

 

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.darksky.net/forecast/d08b7c9c09772f23ae66e4ea807e1de7/lat,long?exclude=flags,alerts,minutely. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

*lat and long are latitude and longitude. i am 100% certain those work

 

 

Link to comment
https://linustechtips.com/topic/911247-fetch-api-blocked/
Share on other sites

Link to post
Share on other sites

Also https://darksky.net/dev/docs/faq#cross-origin

Quote

Your API call includes your secret API key as part of the request. If you were to make API calls from client-facing code, anyone could extract and use your API key, which would result in a bill that you'd have to pay. We disable CORS to help keep your API secret key a secret.

Might want to delete that part of your original post...

Link to comment
https://linustechtips.com/topic/911247-fetch-api-blocked/#findComment-11199023
Share on other sites

Link to post
Share on other sites

1 hour ago, HarryNyquist said:

What's your code look like?

// Darksky 
      fetch(darksky)
         .then(response => {
            if (response.ok) {
               return response.json();
            }

            return Promise.reject(Object.assign(), response, {
               status: response.status,
               statusText: response.statusText
            })
         })
         .then(data => {
            console.log(data);
            handleWeatherData(data);
         })
         .catch(error => {
            console.log(error);
         })

i know i am console logging a lot, this is prototype 

1 hour ago, HarryNyquist said:

Also https://darksky.net/dev/docs/faq#cross-origin

Might want to delete that part of your original post...

yea i noticed that after posting. i requested another key and closed the existing 

Link to comment
https://linustechtips.com/topic/911247-fetch-api-blocked/#findComment-11199274
Share on other sites

Link to post
Share on other sites

1 hour ago, HarryNyquist said:

Make the call on the server-side and it should work. Grabbing the data from the web from, say, a PHP script you write, does not have CORS restrictions attached to it. Calling it from client-side (web browser) is subject to CORS.

so i would need a backend. how should i deploy it?

Link to comment
https://linustechtips.com/topic/911247-fetch-api-blocked/#findComment-11202998
Share on other sites

Link to post
Share on other sites

On 3.4.2018 at 8:27 PM, Technicolors said:

so i would need a backend. how should i deploy it?

Do you use a webserver for your page which has PHP?

Then you can get the data by using cURL:

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => 'YOUR_URL'
));
$response = curl_exec($curl);
curl_close($curl);

//do stuff with $response
?>

 

for(var i = 0; i<= 1/0; i++) {
	alert("Look, i am like while(true){}");
}

1/0 != Error; 1/0 == ∞

Link to comment
https://linustechtips.com/topic/911247-fetch-api-blocked/#findComment-11211103
Share on other sites

Link to post
Share on other sites

4 hours ago, prgm0 said:

Do you use a webserver for your page which has PHP?

Then you can get the data by using cURL:


<?php
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => 'YOUR_URL'
));
$response = curl_exec($curl);
curl_close($curl);

//do stuff with $response
?>

 

i'm new to PHP so i thought of using node

Link to comment
https://linustechtips.com/topic/911247-fetch-api-blocked/#findComment-11211885
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×