0

I have followed a tutorial (and read many others) on making cross-domain requests, but I can't get it to work. I keep getting the same error:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is the code I'm working with. I'm trying to hit the coinbase API.

// Create the XHR object.
  function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
      // XHR for Chrome/Firefox/Opera/Safari.
      xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
      // XDomainRequest for IE.                                                                                                                                                         xhr = new XDomainRequest();
      xhr.open(method, url);                                                                                                                                                          } else {
      // CORS not supported.                                                                                                                                                            xhr = null;
    }                                                                                                                                                                                 return xhr;
  }

// Make the actual CORS request.
  function makeCorsRequest() {                                                                                                                                                        // All HTML5 Rocks properties support CORS.
    var url = 'https://www.coinbase.com/api/v1/prices/historical';
    var xhr = createCORSRequest('GET', url);                                                                                                                                          if (!xhr) {
      alert('CORS not supported');                                                                                                                                                      return;
    }                                                                                                                                                                                                                                                                                                                                                                   // Response handlers.
    xhr.onload = function() {
      var text = xhr.responseText;                                                                                                                                                      var title = getTitle(text);
      alert('Response from CORS request to ' + url + ': ' + title);
    };

    xhr.onerror = function() {
      alert('Woops, there was an error making the request.');
    };

    xhr.send();
  }

  makeCorsRequest();
The Puma
  • 1,352
  • 2
  • 14
  • 27
  • possible duplicate of [No 'Access-Control-Allow-Origin' header is present on the requested resource.' Why is it not showing when I use POSTMAN?](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource-w) – Ray Nicholus May 05 '14 at 01:59

1 Answers1

0

By default, you're not allowed to make cross-domain AJAX calls. If the target defines a CORS policy, then this rule may be relaxed. Details.

If you control the target, you should be able to get this to work by adding a CORS policy.

user3553031
  • 5,990
  • 1
  • 20
  • 40