Jin Sheng
Jin Sheng

Reputation: 27

firebase api for angularjs

I am using firebase for my web and android app. Now, I am trying to call http GET function from firebase to retrieve data but it returns this error message

"Access to XMLHttpRequest at 'https://example.firebase.com' (redirected from 'https://example.firebase.com/') from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

 $http.get('https://example.firebase.com').then(function(response){
    $ctrl.tempData = response.data;
    console.log('response data', response.data);
 }

I found something called curl in somewhere else, but how to actually apply on this?

Upvotes: 0

Views: 120

Answers (1)

Ronnie Smith
Ronnie Smith

Reputation: 18545

If you want to allow CORS it's configurable on Hosting via the firebase.json file. However, the JavaScript SDK (web client sdk) is the way most people interact with Firebase from the browser.

"hosting": {
  // Add the "headers" attribute within "hosting"
  "headers": [ {
    // Specifies a CORS header for all font files
    "source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
    "headers": [ {
      "key": "Access-Control-Allow-Origin",
      "value": "*"
    } ]
  }, {
    // Overrides the default 1 hour browser cache with a 2 hour cache for all image files
    "source": "**/*.@(jpg|jpeg|gif|png)",
    "headers": [ {
      "key": "Cache-Control",
      "value": "max-age=7200"
    } ]
  }, {
    // Sets the cache header for 404 pages to cache for 5 minutes
    "source": "404.html",
    "headers": [ {
      "key": "Cache-Control",
      "value": "max-age=300"
    } ]
  } ]
}

Upvotes: 1

Related Questions