2

I am trying to send parameters from my angular app using httpRequest. I am getting back Null to my backend server. I have checked with Postman and Fiddler both work with a json Object. I have tried changing from Post to Get. I am using Java RestAPI for the backend with apache Tomcat as the server.

This is my Service for login:

@Injectable({
  providedIn: 'root'
})
export class LoginService {
private loginURL='http://localhost:8080/CouponSystemWeb/rest/loginpage/login'
  constructor(private http:HttpClient) { }

 public login(loginDetailes:LoginDetailes):Observable<LoginDetailes>{
  return this.http.post<LoginDetailes>(this.loginURL,loginDetailes,{withCredentials:true})
 } 
}

This is my Login Component:

import { Component, OnInit } from '@angular/core';
import { LoginDetailes } from 'src/app/Entities/LoginDetailes';
import { LoginService } from 'src/app/services/login.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
public loggedin:boolean;
public loggedClient:string;
public errormessage:string;
public loginDetailes = new LoginDetailes();
  constructor(private loginservice:LoginService,private router:Router) { }

  ngOnInit() {
  this.loggedin=false;
  }
public onLogin():void{
  const observable=this.loginservice.login(this.loginDetailes);
  observable.subscribe((returnedLoginDetailes:LoginDetailes)=>{
    alert("Login Aquired");
    this.loggedin=true;

  if(this.loginDetailes.clientType=="ADMIN"){
    this.router.navigate(['/crtComp']);
  }
  else if(this.loginDetailes.clientType=="COMPANY"){
  this.router.navigate(['/login']);
  }
  else if(this.loginDetailes.clientType=="CUSTOMER"){
    this.router.navigate(['/login']);
  }else{
    alert("Wrong Login Detailes");  
  }

  }, err => {
  this.errormessage=err.console.error("Wrong Detailes please Check Again!");
  alert(this.errormessage);
}
  )}} 

This is the login Entity :

export class LoginDetailes{
    public name:string
    public password:string
    public clientType:string
    constructor(){
    }
}

I have tried ngModel but that didn't fix the problem. I have tried changing my backend from Post to Get. The problem happends only in the angular App. I can send parameters with fiddler and Postman without problem.

3
  • dont know why it got into java ill edit this out. Commented Feb 24, 2019 at 6:53
  • It seems I are passing blank login details from your component to service Commented Feb 24, 2019 at 7:30
  • @MukulSharma can you elaborate on this? im quite new to angular Commented Feb 24, 2019 at 7:33

1 Answer 1

1

Ok the answer was not in the component or the service. the problem was in the HTML i was missing the ngModel two way data binding so my App was sending null's.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.