1

Hello I have this code:

save<T>(url: string, data:any, headers = null): Observable<T>{
return this.http
            .post(url, data, headers)
            .map(x =>{
                return x.json() as T
            } )
            .catch(error => {
                this.app.handleError(error, errorMessage);
                return Observable.throw(error);
            }).share();
}

And when i send request to action which return empty response (_body: "") I have error SyntaxError: Unexpected end of JSON input in x.json()

How to check when body isnt empty? Thank you

0

2 Answers 2

1

Try this

.map(x =>{
  return (x._body ? x.json() : null) as T
})
Sign up to request clarification or add additional context in comments.

Comments

1

Test this,

.map(x =>(x._body ? x.json() : null) as T)

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.