0

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?

2
  • Why are you not using the Firebase JavaScript SDK that's meant for use in web applications? That's the preferred way to access Firebase. Or AngularFire? Commented Dec 10, 2018 at 2:21
  • At the very least you'll want to make sure the URL ends in .json, since you're trying to access the REST API. So $http.get('https://example.firebase.com/.json').... Commented Dec 10, 2018 at 3:14

1 Answer 1

1

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"
    } ]
  } ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.