Response body from post request in angular is empty

i am doing a post request using Http in angular 7,

code:

signInUser(reqData) {
const headerInfo = new Headers({
                                            'Content-Type': 'application/x-www-form-urlencoded',
                                            'X-TIME-ZONE': '+5.30',
                                            'X-DEVICE-ID': '911422351920594',
                                            'X-APP-VERSION': '45',
                                            'X-APP-CHANNEL-NAME': 'ANDROID',
                                            'X-DEVICE-OS-VERSION': '45',
                                            'X-DEVICE-MODEL': 'ANDROID'
                                            });
    const request = this.http.post('server-ip', reqData, {headers: headerInfo});

    request.subscribe(
      (response) => {
        console.log(response);
      }
    );
}

i am having backend in spring mvc, and the request is working fine and the server has generated the required response. In the server log i can see the response json data.

but when it comes to angular, the response body is empty.

angular response body

why is this happening?

Upvotes: 0

Views: 2381

Answers (2)

Sahan.java
Sahan.java

Reputation: 70

Add @CrossOrigin annotation to your rest controller.

@CrossOrigin
@RestController
public class controller{
//..............
}

Upvotes: 0

wentjun
wentjun

Reputation: 42526

You will need to return the http request.

signInUser() {
  .
  .
  return this.http.post('server-ip', reqData, {headers: headerInfo});
}

And on your component that requires it, you will subscribe to it in order to return the observable value.

this.service.signInUser().subscribe(response => {
  console.log(response);
});

Upvotes: 1

Related Questions