On form submission I would like to stay on the same page and display a message (either success or failure). The HTTP POST
request is handled as follows:
app.post('/insertdb', function(request, response) {
// insert values from form into database
response.json({success: true});
});
In the client side .ejs
file I then try to use ajax to display the alert (I tried following the example from how to use NodeJS pop up a alert window in browser) as follows:
<script>
$.ajax({
url: "/dbinsert",
type: "post",
data: 'testing',
cache: false,
success: function(response) {
if(data['success']) {
alert("success");
}
},
error: function () {
alert("There has been an error");
}
});
</script>
The issue is that when I press the submit button it takes me off the page and displays: ```{"success":true}`` instead of staying on the same page after the form submission and displaying a popup with the status of the request.