How to send Form data to Servlets through Ajax
Hi ,
I am trying to send form data to servlets (path based servlet ) using ajax. Here I have written the code but it's not working. When I submit the form nothing is happening just it refreshes the page. And I don't see any form data in servlet path. Any idea how to resolve this ??
<button data-sly-use.button="com.adobe.cq.wcm.core.components.models.form.Button"
type="${button.type}"
id="${button.id}"
onclick="doCall()"
class="cmp-form-button"
name="${button.name}"
value="${button.value}">${button.title}</button>
<script>
function doCall() {
var details = {"firstname": $("#firstname").val()};
var userJson = JSON.stringify(details);
alert(userJson);
console.log(userJson);
var url = "/bin/form";
$.ajax({
url: url,
type: "POST",
data: userJson,
contentType: "application/json",
error: function(message) {
console.log(message);
},
success: function(data) {
console.log(data);
}
});
}
</script>