This is my code for sending form data to nodejs using Ajax. I want to achieve this without reloading the page. I have tried to run this in IE,Firefox and Chrome. There is no output seen, hence I want to know where am I going wrong. I am new to Ajax, please help me!
<html>
<meta charset="UTF-8">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$('#newform').on('submit', function (event) {
//alert('Hi');
event.preventDefault();
var data = {
username: $('#username').val(),
};
$.ajax({
url: 'http://127.0.0.1:8081/upload',
data: data,
method: 'POST'
}).then(function (response) {
$('body').append(response);
}).catch(function (err) {
console.error(err);
});
});
</script>
</head>
<body>
<form action="/upload" method="post" id="newform">
<input type="text" name="username" id="username"></input>
<input type="button" value="submit" id="s1"></input>
</form>
</body>
</html>