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() {
}
}