SimplerWeather icon indicating copy to clipboard operation
SimplerWeather copied to clipboard

Access-Control-Allow-Origin

Open alexis3290 opened this issue 7 years ago • 10 comments

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 :

  • [Error] Origin https://www.mydomain.com is not allowed by Access-Control-Allow-Origin.
  • [Error] XMLHttpRequest cannot load https://api.darksky.net/forecast/748b8d1e668697d8a91f8ebdd1ecf5ea/40.785091,73.96828/?units=us&exclude=minutely,hourly,alerts,flags due to access control checks.
  • [Error] Failed to load resource: Origin https://www.mydomain.com is not allowed by Access-Control-Allow-Origin. (40.785091,73.96828, line 0)

Thank you for your work !

alexis3290 avatar Jan 12 '19 14:01 alexis3290

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

s3bast avatar Jan 14 '19 19:01 s3bast

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';

alexis3290 avatar Jan 15 '19 08:01 alexis3290

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 ) { ... }

bgallagh3r avatar Jan 21 '19 19:01 bgallagh3r

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.

alexis3290 avatar Jan 23 '19 10:01 alexis3290

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

bgallagh3r avatar Jan 23 '19 12:01 bgallagh3r

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>'}
});

alexis3290 avatar Jan 23 '19 12:01 alexis3290

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'

bgallagh3r avatar Jan 23 '19 13:01 bgallagh3r

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

alexis3290 avatar Jan 23 '19 14:01 alexis3290

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 avatar Jan 23 '19 14:01 bgallagh3r

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.

bgallagh3r avatar Jan 23 '19 14:01 bgallagh3r