I am new to Angular2. While trying to get the values from Firebase API , I am getting below Error:
OPTIONS https://udemy-ng-http.firebaseio.com/data 405 (Method Not Allowed)
Failed to load https://udemy-ng-http.firebaseio.com/data: Response for preflight has
invalid HTTP status code 405
But I am able to post the data to Firebase API Endpoint.
I also tried other solutions provided on this community but it did not help. Below is my code.
Code:
getServer(){
let headers = new Headers();
headers.append('Access-Control-Allow-Headers', 'Content-Type');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Content-Type', 'application/json') ;
headers.append('charset','UTF-8');
headers.append('Accept','application/json');
let options = new RequestOptions({ headers: headers });
return this.http.get('https://udemy-ng-http.firebaseio.com/data',options)
.map(
(response: Response) => {
const data = response.json();
for (const server of data) {
server.name = 'FETCHED_' + server.name;
}
return data;
}
)
.catch(
(error: Response) => {
return Observable.throw('Something went wrong');
}
);
}
Try removing headers.append('Access-Control-Allow-Origin', '*');
There are two sample functions provided by the Firebase team that demonstrate the use of CORS:
The second sample uses a different way of working with cors than you're currently using.
Also, consider importing like this, as shown in the samples:
const cors = require('cors')({origin: true});