BrookeDot / SimplerWeather

jQuery Weather Plugin using Dark Sky –– Fork of simpleWeather
MIT License
20 stars 9 forks source link

Access-Control-Allow-Origin #5

Open alexis3290 opened 5 years ago

alexis3290 commented 5 years ago

Hello,

I arrive here following the end of Yahoo API support on SimpleWeather. I currently trying to fix this with your repo but I facing an issue :

Thank you for your work !

s3bast commented 5 years ago

Same here! I use https://cors-anywhere.herokuapp.com/ in front of the api.darksky url and it work. But not for IE/Edge.

alexis3290 commented 5 years ago

Don't understand how it solve the problem 'cause on Dark Sky FAQ, they said :

As a security precaution we have disabled cross-origin resource sharing (CORS) on our servers. https://darksky.net/dev/docs/faq#cross-origin

So I set up the proxy.php but I don't have any return from the Dark Sky API (the good news is I don't have CORS error anymore :) )

Is it the right way to setting up $api_key = 'MYAPIKEY'; $api_endpoint = 'https://api.darksky.net/forecast/MYAPIKEY/50.8733618,4.5032048';

bgallagh3r commented 5 years ago

I decided to post here to give you come clarification as I came here due to the Yahoo API going down too.

Dark Sky does NOT allow the client to make a direct API call as this would expose the API key to the public which would rack up a lot of cash on your account. I'm not sure why they don't allow you to specify a domain in your account settings, it would have solved the Cross Origin issue on their end, but I guess more work than they want to do.

Anyways what you need to do is define the authmethod as proxy and provide a proxyurl in the options to point to the proxy.php script or a url for the server to process the request and send back the data.

$.simplerWeather( {
  location: '40.785091,73.96828',
  authmethod: 'proxy',
  proxyurl: 'http://example.com/proxy.php'
  success: function( weather ) { ... },
  error: function( error ) { ... }
alexis3290 commented 5 years ago

Understood that, but for me the proxy.php return me the message "URL format invalid"

And it's exactly the same from the repo except I add my API key. And when I'm going on Darksky, the API request are counted … that's strange …

{EDIT} And in the consol on the webpage where I'm trying to print the weather, I have the following error SyntaxError: Unexpected identifier 'success'. Expected '}' to end an object literal.

bgallagh3r commented 5 years ago

Sounds like you have an invalid character in your JS. Can you post your code examples?

alexis3290 commented 5 years ago

For sure ! here it is

$.simplerWeather( {
  location: '40.785091,73.96828',
  authmethod: 'proxy',
  proxyurl: 'https://www.mydomain.com/js/proxy.php'
  success: function( weather ) {'<p>YES BABY</p>'},
  error: function( error ) {'<p>ERROR BABY</p>'}
});
bgallagh3r commented 5 years ago

For sure ! here it is

$.simplerWeather( {
  location: '40.785091,73.96828',
  authmethod: 'proxy',
  proxyurl: 'https://www.mydomain.com/js/proxy.php'
  success: function( weather ) {'<p>YES BABY</p>'},
  error: function( error ) {'<p>ERROR BABY</p>'}
});

Looks like you're missing the comma after proxy.php'

alexis3290 commented 5 years ago

OMG, so stupid 🤦🏻‍♂️ Don't have an error in the console anymore, but still not working.

Tha problem probably comes from the proxy.php, when I visit proxy.php, I've got URL format invalid but it's exactly the same proxy.php from the repo, I only edit the API key

bgallagh3r commented 5 years ago

If you visit the proxy.php file directly you will need to include the lat/lon in the get request in order to see the data.

https://example.com/js/proxy.php?lat=40.785091&lon=73.96828

As the proxy.php passes the lat/lon from the get request to the API I'm sure darksky will throw an error if they are not valid coords. Try that.

bgallagh3r commented 5 years ago

Honestly I think they should just remove the apikey method in this fork as you can't actually use it now that Darksky has disabled Access Control Allow Origin on their end. I actually just ended up removing this library all together and wrote my own PHP calls to fetch/display the data. Since the server is fetching the data, I cache it and just display it without having to make an extra AJAX request.