I am trying to perform the following post request in my ionic app , where I get following error
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.
But same request works perfectly when performed using postman in the same PC. Appreciate your valuable help.
Postman Response
Ionic HTTP Post request
import {
HttpClient,
HttpHeaders
} from '@angular/common/http';
constructor(public menuCtrl: MenuController, private http: HttpClient, private router: Router, private alertController: AlertController,
public loadingController: LoadingController) {
window.sessionStorage.setItem("ip", "myapi.com");
}
let authorizationData = 'Basic ' + btoa(this.username.trim() + ':' + this.password.trim());
window.sessionStorage.setItem("auth", authorizationData);
const httpOptions = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json; charset=utf-8',
'Authorization': authorizationData
})
};
console.log(authorizationData);
this.present();
this.http.post('https://' + window.sessionStorage.getItem('ip') + '/OffsiteAtmMonitoring/login/submit', {}, httpOptions)
.subscribe(
data => {
window.sessionStorage.setItem("name", data["name"]);
window.sessionStorage.setItem("firstname", data["firstname"]);
window.sessionStorage.setItem("lastname", data["lastname"]);
window.sessionStorage.setItem("username", data["username"]);
}