I have the following form and I need to make AJAX call on submission of the form. How to do in jQuery by serializing?
<form class="form-horizontal"> <fieldset> <legend>Please Fill in the Online Form</legend> <div class="form-group"> <label for="inputEmail" class="col-lg-2 control-label">Name</label> <div class="col-lg-10"> <input type="text" class="form-control formWidth" id="inputEmail" placeholder="Name" required> </div> </div> <div class="form-group"> <div class="col-lg-10 col-lg-offset-2"> <button type="reset" class="btn btn-default">Cancel</button> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </fieldset> </form> My request response format is:
http://10.23.25.36:8081/mytest/user/register {"name":"abcd","email":"[email protected]","phone":"12356984"} { "message": "SMS SENT", "status": "SUCCESS", } What's wrong with this?:
$(document).ready(function() { alert("hello"); $('.form-horizontal').on('submit', function(e){ alert("inside form submit"); var postData = $(this).serialize(); var formURL = "http://10.23.25.36:8081/mytest/user/register"; $.ajax({ url : formURL, type:"POST", data : postData, dataType:"json", done:function(msg){ setSuceessMsg(msg); }, fail:function(jqXhr, textStatus){ alert("Error in response" + textStatus); }, always:function(msg){ alert("In always"); } }); e.preventDefault(); //STOP default action //e.unbind(); //unbind. to stop multiple form submit. }); $("#ajaxform").submit(); //Submit the FORM (function ( $ ) { $.fn.setSuceessMsg = function(msg) { return this; }; }( jQuery )); });