0

Apologies in advance, this is a real 'training wheels question'. I am following the instructions on the FRED API (Federal Reserve Bank of St.Louis) website for the HTTP Get request and I keep getting cross-origin errors, but can't find any documentation on how to fix the code.

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://api.stlouisfed.org/fred/releases/dates?api_key=1cee0e87e5e3362716028352d3d1c160", false );
xmlHttp.send( null );
return xmlHttp.responseText;

Everything syntaxically is correct, I thought using the site-defined API key would be the only 'authentication' and would let me through?

This is the example link: St.Louis Fed Web Services I have been googling/messing with code for the last 5 hours, can anyone weigh in? thanks

  • Possibly the site does not implement a [cross site access](https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS)? – Passerby Dec 26 '13 at 04:22
  • 1
    Your code looks fine (although I would suggest using [JQuery](http://api.jquery.com/jquery.get/) to abstract away the details of the XHR). I don't think FRED has enabled CORS as described [here](http://stackoverflow.com/questions/19821753/jquery-xml-error-no-access-control-allow-origin-header-is-present-on-the-req). – Vidya Dec 26 '13 at 04:27

1 Answers1

1

Web browsers prohibit cross-domain http requests unless the service you request the data from explicitly allows it, which is specified in the "control access allow origin" header on the http response. Unfortunately, FRED has chosen not to allow them. You aren't doing anything wrong in the code above, there's just no way to do this in a web browser.

I think the goal for the FRED API is to allow researchers to download the data directly into statistical applications like STATA and SAS. They apparently don't want people using the API to build web applications.

Matthew
  • 4,149
  • 2
  • 26
  • 53