Error connection with my Rest API


#1

I can not communicate with my api in the server
i get this message error
no ‘access-control-allow-origin’ header is present on the requested resource
Login.ts

 public login() {
    this.showLoading()
    this.auth.login(this.registerCredentials).then((result) => {
      this.nav.setRoot(HomePage);
    }, (error) => {
      this.showError(error);
    });
  }

auth.ts (login Service)

 login(credentials){
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      this.http.post(apiUrl , JSON.stringify(credentials), {headers: headers}).
      subscribe(res =>{
        resolve(res);
      }, (err) =>{
        reject(err);
      });
    });
  }

#2

This is likely due to a CORS issue, which you need to resolve at server side. There is an abundance of postings on this forum telling you how to solve it at server side

Secondly, your login pattern seems a bit over the top in terms of coding. First of all, why convert an Observable to a Promise (Observables are the next-gen Promise)? Secondly, if you insist, you can do it like this (and not sure why you need to incllude empty headers)

 login(credentials){
    return 
      this.http.post(apiUrl , JSON.stringify(credentials))
              .toPromise()
  }

#3

tkx a lot i’ll try the CORS issue
i used promise cuz i just want to resolve the probleme if not I did as you did
for the headers i will fill it after