Http post ionic4

I am Using Post Data With Http In ionic 4. My Sample Data Is Like this,
{
“employee”: {
“name”: “abcd”,
“mobile”: “8229678323”
}
}
my Code,

import { Http , Headers } from ‘@angular/http’;

var data = JSON.stringify({ employee: { name: “abcd”, mobile: “8229678323”}});

let headers = new Headers();
headers.append(‘Content-Type’, ‘application/json’);
let options = { headers: headers };

this.http.post(link,data,options).subscribe(data =>{
alert(data);
},error => {
alert("Error "+ error.error);
});

I Got Error Undefined. Please Help me to Solve this.

Are you using cordova-plugin-advanced-http, I imagine.

You have not to stringify the object, but pass the objects to the plugin.

In the constructor remember to put:

constructor(private http: HTTP)  {
     http.setDataSerializer('json')
}

see here

Pietro

PS: please use the formatter for code, the </> icon.

Thanks And I Will Try.

Before you start worrying about cordova plugins, I would suggest simply trying HttpClient.

2 Likes

Thank You pimol it is Working…