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

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

1 Answer 1

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>

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.