Reputation: 837
I am getting Access-Control-Allow-Origin error.
Access to XMLHttpRequest at 'https://localhost:44301/api/XXXX/GetAllXXXX' from origin 'https://localhost:44322' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
And below is my header which i have passing to api call.
getAllItems<T>(): Observable<T> {
const options = { headers: this.getRequestHeaders() };
return this.http.get<T>(this.getAllItemUrl, options);
}
protected getRequestHeaders(): HttpHeaders {
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Accept': `application/json, text/plain, */*`,
'App-Version': '1',
});
return headers;
}
Am i missing anything to connect my API's here?
Upvotes: 0
Views: 6720
Reputation: 41
You need to add CORS to your backend service. If its an express based service you can have something like this
const express = require('express');
const app = express();
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
app.use(allowCrossDomain);
Upvotes: 1