Typescript post request not entering controller, but Postman works

115 Views Asked by At

Angular 15, dotnet core 7

POSTMAN: does a post of https://localhost:7270/api/Account/login the login parameter is a json with username and password

It runs in the controller just fine and returns correct data.

Angular Typescript creates a loginDto with username and password with identical data as used in POSTMAN, I put a breakpoint in the controller constructor, which is never reached.

Chrome shows the error response is "... Unknown Error". The URL matches what was sent by POSTMAN.

Swagger works fine as well.

How can I debug this error?

***** Updates per your requests ******* Typescript:

export class loginDto 
{
    public Username!: string;  
    public Password!: string;

    constructor(user: string, password: string)
    {
        this.Username = user;
        this.Password = password; 
    }
}

*** Calling function:

async loginAsync(userName: string, pass: string) {
    this.baseUrl = this.cache.getBaseUrl(); <<--- see below

    var dto: loginDto = new loginDto(userName, pass);

    this.http.post<UserDto>(this.baseUrl + 'account/login', dto).subscribe({
        next: (response: UserDto) => {
          this.toastr.success("Logged In Successfully", "", { positionClass: 'toast-bottom-right' });
          var username = response.Username;
          var token = response.Token;
          var knownAs = response.KnownAs;
          var roles = "admin";           // add roles when identity is added
          this.cache.cacheUser(username, token, roles, knownAs);
     
          this.route.navigateByUrl('/splashPagePost');
        },
        error: (error) => {
            this.toastr.error("Invalid Login", "",  { positionClass: 'toast-bottom-right' });
            this.cache.setUserLoginStatus('NavVisitor');
            this.accountLoginKnown.next('');
            this.route.navigateByUrl('/splashPage');
          }
        }
      );
    }

*** The baseUrl comes from environment.ts: apiUrl: 'https://localhost:7270/api/'

*** Since you asked me to look at the network, I see more info here: enter image description here

*** Program.cs

enter image description here

I'm not aware of anything I should put into Angular side for CORS?

***** HERE is the FULL Network screen for this request

enter image description here

***** opening the CORS error line in Network:

enter image description here

This is correct...

**** Opening the Next network line (as shown)

enter image description here

0

There are 0 best solutions below