Hi guys,
I use native http for post request
How display successfully message in HTML file And pop the window
Greetings,
You could use Toast or Alert component to display message.
I am new with ionic, so please can you give an example?
Post you code sample here so I could create one or you could just follow ionic documentation, itâs pretty easy.
1 Like
hi this is my code:
sendPostRequest() {
this.resetError()
if (this.isDataValid()) {
this.http.sendRequest(âmyapi/createcommentâ,
{
method: 'post',
data: { id: this.id, Name: this.Name, EmailFrom: this.EmailFrom, text: this.text },
headers: { Authorization: 'OAuth2: token' },
timeout: 5000
}
)
.then(response => {
// prints 200
console.log(response.status);
})
.catch(response => {
// prints 403
console.log(response.status);
// prints Permission denied
console.log(response.error);
});
}
}
This combination concerns me. Why arenât you using Angularâs HttpClient?
1 Like
import { ToastController } from '@ionic/angular'; // Added Line
constructor(public toastController: ToastController) {} // Add in constructor
sendPostRequest() {
this.resetError()
if (this.isDataValid()) {
this.http.sendRequest(âmyapi/createcommentâ,
{
method: 'post',
data: { id: this.id, Name: this.Name, EmailFrom: this.EmailFrom, text: this.text },
headers: { Authorization: 'OAuth2: token' },
timeout: 5000
})
.then(response => {
// prints 200
this.presentToast('Success Message') // call toast
console.log(response.status);
})
.catch(response => {
// prints 403
this.presentToast('Error Message') //Call toast
console.log(response.status);
// prints Permission denied
console.log(response.error);
});
async presentToast(msg) {
const toast = await this.toastController.create({
message: msg, //Custom Message
duration: 2000 //Duration
});
toast.present();
}
}
Hope this will help.
1 Like