I have a simple code, and I try to make a post without redirecting the page. So far my code looks like this:
$(function() {
$('#btn').on('click', '.document', function(e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '/save',
data: {
html: '<p>Sucess</p>',
delay: 1
},
dataType: 'html',
success: function(data) {
console.log('success');
$('.document').append(data);
},
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form action="/save" method="post" autocomplete="off" id="theForm">
<button id="btn" type="submit" name="send">
Send
</button>
</form>
<span class="document"></span>
Can anybody try to help me with this? I don't know why my code doesn't work, I want to be able to POST
without reloading and redirecting the page.