0

Am trying to use firebase cloud functions in my Angular5 web app, but i keep receiving the error bellow:

Failed to load https://us-central1-xxxxx.cloudfunctions.net/test: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.

I've been searching for a week to solve the issue, but i couldn't find any solution, any help please?! Thanks in advance.

This is my code:

var functions = require('firebase-functions');

exports.test = functions.https.onRequest((req,res)=>{
  res.send("TEST")
})

The angular Component

import { Component, OnInit } from '@angular/core';
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
import { RequestOptions } from '@angular/http';
import {HttpHeaders, HttpParams} from "@angular/common/http";

@Component({
  selector: 'app-welcome',
  templateUrl: './welcome.component.html',
  styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent implements OnInit {


  constructor(private http: Http) { }

  sendEmail() {

    let url = `https://us-central1-XXXXX.cloudfunctions.net/test`
    let params: URLSearchParams = new URLSearchParams();
    let headers = new Headers({'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });
    let options = new RequestOptions({headers :headers})
    
    return this.http.post(url, params,options)
      .toPromise()
      .then( res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })

  }

  ngOnInit() {
  }


}
A.Queue
  • 1,549
  • 6
  • 21
  • Thanks ! But still the same problem ! – Adel Azzouza Feb 24 '18 at 19:41
  • If you are testing your function locally it could be that browser is blocking [CORs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). [Here](https://stackoverflow.com/questions/48946168/failed-to-load-no-access-control-allow-origin-header-is-present-on-the-request) a solution was to disable browser's security. – A.Queue Mar 06 '18 at 08:27

0 Answers0