0

I'm using Angular 6 to get a string from my Java REST Application and can't manage to parse/get the string

The REST API Service works just fine, it returns the String as expected, but in the Browser console I get this error

"SyntaxError: Unexpected token X in JSON at position 4 at JSON.parse"

I think this is linked to the Content-type of the Header

I've already tried to change the response type, but I can't send my data then.

login(userDTO:UserDTO):Observable<String> {
    let header:HttpHeaders = new HttpHeaders({
      'Content-type' : 'application/json',
    });
    return this.httpClient.post<String>('http://localhost:8080/login',  
userDTO, {headers: header});
  }

EDIT: "Where are you getting the String object from? You should be using the string literal. – Edric 2 mins"

I'm getting it from a REST Java service which returns a String Java Object

ayyylmao
  • 71
  • 1
  • 13
  • 1
    Where are you getting the `String` object from? You should be using the `string` literal. – Edric Jan 14 '19 at 11:35
  • See the duplicate I've linked. In your case, remove the `` and add the `responseType` property: `return this.httpClient.post('http://localhost:8080/login', userDTO, {headers: header, responseType: 'text'});` – Kirk Larkin Jan 14 '19 at 12:00

1 Answers1

1

You need to expect string object not String object. Just change

return this.httpClient.post<String>('http://localhost:8080/login',
userDTO, {headers: header});

to

return this.httpClient.post<string>('http://localhost:8080/login',
userDTO, {headers: header});

Expect return type also as Observable<string>

Ganesh
  • 5,808
  • 2
  • 21
  • 41