Error during POST request in ionic

I’m trying to make a POST request to a server I’ve created. But when I run the application, I get the error :
" Unexpected token L in JSON at position 0"

This is the code I’m using:

import { Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';

export class MyClass {
constructor(public http : Http){

let headers = new Headers();
headers.append('Content-Type', 'application/json');

let body = {
   message : "Do you hear me?"
};
this.http.post('https://myserverurl', JSON.stringify(body), {headers:headers})
  .map(res=>res.json())
  .subscribe((data)=>{
  console.log(data);
  });
 }
}

Appreciate any help anyone has to offer. Thanks !

I think put double quotes in json key an value pair
IE:

let body = {
    "message" : "Do you hear me?"
};

Didn’t work. And I’m anyway converting the object to JSON format using

JSON.stringify(body);

Try

Console.log(res)

Before res.json()

And take a look at the server response.

The server is giving the right response. I’m getting the message “Loud and clear” successfully. The problem is this weird error on the client side. I even checked if the body is getting properly stringified and that’s also fine. Still don’t know what’s causing the error

Loud and clear is no valid json

So res.json() throws an error

I think there is a res.text() function you can try instead.

Yup! Good catch! Worked fine :slight_smile: Thanks a lot!

You are welcome. You can mark the answer as solution so the other users see that here is nothing more to do.