Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular can't get response status text

export class GitComponent implements OnInit {
  http: HttpClient;
  headerForAjax: HttpHeaders;

  constructor(http: HttpClient) {
    this.http = http;
  }

  ngOnInit(): void {
    const headers = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzYXNobyIsImF1dGgiOiJST0xFX1VTRVIiLCJleHAiOjE1MjQwODcyMzJ9.MUv5RgI9LxQyrrCfjfX8HR2-XiQmz4vjLqH7V_0Du7VFLC0WrK_y3FfeNoT2Nj_uguIK2ss7jv-LNiHuCGtz4A';
    this.headerForAjax = new HttpHeaders().set('Authorization', headers);
    const linkBindingModel = {
      title: 'angular2',
      linkUrl: 'angularUr2l',
      imageUrl: 'imageUrl22'
    };
    this.http.post('http://localhost:8080/links/add', linkBindingModel, {headers: this.headerForAjax}).subscribe((e) => {
      console.log(e);
    });
  }
}

So this ajax is send to my spring server and the server saves the data correctly in the DB, it basically all works well.

But i can't get the response status, i mean how can i get the 200 response status number or text that my server sends back?

like image 823
Alexander Avatar asked Mar 01 '26 04:03

Alexander


1 Answers

try this, you can pass in an object with observe key to get the complete response

this.http.post('http://localhost:8080/links/add', linkBindingModel, {headers: 
  this.headerForAjax, observe: 'response'}).subscribe((response) => {
  console.log(response.status); // response status 
  console.log(response.body); // response body (returned response)
});
like image 144
Fateh Mohamed Avatar answered Mar 02 '26 16:03

Fateh Mohamed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!