1

I'm unable to retrieve data from the Rescue Time API. I'm making a request in a JavaScript file using the jQuery get() method. Here is a look at the JavaScript related to the API GET request:

$.get('https://www.rescuetime.com/anapi/data?key=########################&format=json&restrict_kind=overview', function(data) {
  // callback function code...
});

The "key=########################" is the paramater that includes my API key.

When running the script (either locally or on my personal domain), I receive a cross origin error:

XMLHttpRequest cannot load https://www.rescuetime.com/anapi/data?key=########################&format=json&restrict_kind=overview. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I understand that this is happening because i'm requesting content that is on a different domain than the one that is making the AJAX request. That being said, how do I get around this? I've read the CORS MDN documentation, but could not decode what actionable steps I need to follow in order to resolve this issue.

I need some actionable steps.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Robert Cooper
  • 2,160
  • 1
  • 9
  • 22
  • They would need to set it on their end.... You can;t do anything from your client side code. – epascarello Jul 25 '17 at 01:17
  • @epascarello What would the API developers need to do? I've linked to other APIs using client side code, but this API is causing me issues with cross origin errors. – Robert Cooper Jul 25 '17 at 01:46
  • Well you can not get around Same Origin Policy and CORs. If they do not support CORs that you can not make an Ajax call. Maybe they support JSONP. Talk to them about CORs.. – epascarello Jul 25 '17 at 01:50

1 Answers1

2

Set up a CORS proxy using the code from https://github.com/Rob--W/cors-anywhere/ or similar.

https://cors-anywhere.herokuapp.com/ is a public instance running that code, and the way you could use it is by changing your existing code to this:

$.get('https://cors-anywhere.herokuapp.com/https://www.rescuetime.com/anapi/data?key=########################&format=json&restrict_kind=overview', function(data) {
  // callback function code...
});

Be aware though that if you do that, your key would potentially be exposed to the operator of that https://cors-anywhere.herokuapp.com/ instance. So if that’s a concern then don’t try it, and instead set up your own proxy at https://some.url.for.your.proxy and change your code to:

$.get('https://some.url.for.your.proxy/https://www.rescuetime.com/anapi/data?key=########################&format=json&restrict_kind=overview', function(data) {
  // callback function code...
});

Either way the result will be that your request gets sent through the specified CORS proxy, which forwards the request to the https://www.rescuetime.com/anapi/data… endpoint and then receives the response. The proxy backend then adds the Access-Control-Allow-Origin header to the response and finally passes that back to your requesting frontend code.

Your browser then allows your frontend code to access the response, because that response with the Access-Control-Allow-Origin response header is what the browser sees. Otherwise, if the response lacks Access-Control-Allow-Origin, browsers won’t let your code access it.

A CORS proxy like that is the only option if you want to make the request from frontend JavaScript code running in a browser, and want to consume the response from that frontend code. Otherwise, without the use of such a proxy, browsers will block your code from accessing the response—because the https://www.rescuetime.com/anapi/data… API endpoint doesn’t itself send the necessary Access-Control-Allow-Origin response header.

Your only other option otherwise is to not make the request from your frontend code but instead make the request from whatever backend server-side code you’re running. In that case there’s no browser in the middle enforcing cross-origin restrictions on the request.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • This has helped me solve my issue! Thank you so much for your detailed explaining. This was my first ever Stack Overflow question and I'm very happy with the feedback/comments that were provided :D – Robert Cooper Jul 25 '17 at 15:45