I'm building my very first web application using Angular 8.
My goal is to use the 'SendGrid' API to process emails.
From what i've read cloud functions is the way to go, so to use them I was attempting doing it by Http protocol from the Angular side and communicate with the cloud engine using NodeJS.
I've tested the cloud function on the google cloud platform and it works, i've also used my link and was able to render the new page with helloWorld.
When executing the simple application in angular I am getting some errors relating to 'cors' and XMLHttpRequest 'No Access Control Allow Origin'
I've been reading on google documentation for http functions
Is there an example I can use to work this out?
a.component.ts
`
import { HttpClient, HttpHeaders } from '@angular/common/http';
......
constructor(private http: HttpClient){}
......
cloudfx(){
const option = {
headers: new HttpHeaders({
'Content-Type': 'text/plain'
})
};
const url = 'https://name-of-server.cloudfunctions.net/helloWorld'
const tGET = this.http.get(url, options);
tGET.subscribe();
console.log(tGET);
}
}`
index.js
`
export const helloWorld = functions.https
.onRequest((request, response) => {
response.set('Access-Control-Allow-Origin','*');
response.set('Access-Control-Allow-Headers','*');
if(request.method == 'OPTIONS'){
response.set('Access-Control-Allow-Methods','GET');
response.status(204).send('');
} else{
response.send('Hello World!');
}
});`