I am facing an issue while trying to post an array of contacts on a WebService.
When I send the array, the data appears as NULL in the WebService response.
I am confused about how to use Let params{}
The error message shows "object undefined".
Additionally, the POST method is not functioning properly.
My technology stack includes Ionic3 - Angular4 and Sqlite.
Here's a snippet from my home.ts file:
public contactos: any [] = [];
constructor(public navCtrl: NavController,
private sqlite: SQLite,
private toast: Toast,
public http: Http) {
}
postRequest(){
this.sqlite.create({
name: 'ionicdb.db',
location: 'default'
}).then((db: SQLiteObject) =>{
db.executeSql('SELECT * FROM Contactos ORDER BY rowid DESC', {})
.then(result => {
this.contactos = [];
for(var i=0; i<result.rows.length; i++) {
this.contactos.push({
rowid:result.rows.item(i).rowid,
nombre:result.rows.item(i).nombre,
telefono:result.rows.item(i).telefono,
ext:result.rows.item(i).ext,
correo:result.rows.item(i).correo,
empresa:result.rows.item(i).empresa})
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let params = {
nombre: 'Sergio',
telefono: '1234567',
ext: '000',
correo: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b011e091c12141f101a16... // abbreviated for brevity
}
this.http.post("http://181.48.244.108/FeriaDutotec/api/Contactos1", JSON.stringify(params), options)
.subscribe(contactos => {
this.contactos.push(contactos.json())
console.log(contactos ['_body']);
this.toast.show('Datos Sincronizados!' , '4000', 'center').subscribe(
toast => {
console.log(contactos);
}
)
}, error => {
console.log(error.message);// Error obteniendo los datos!
this.toast.show('Error en la sincronizacion!'+ error.message, '4000' , 'center').subscribe( // abbreviated for brevity
)
});
}
console.log('Result')
})
.catch(e => console.log(e));
console.log('Create')
})
.catch(e => console.log(e));
}