can't post form data as json using jquery 3 to jersey -
i need understand why $.ajax not sending post instead jersey?
i have seen many posts in stackoverflow , other websites/forums no success.
this client code:
$('#formlookupuser').submit( function (event) { var userfilter = { "email" : $('#inputemail').val(), "firstname" :$('#inputfirstname').val(), "lastname" : $('#inputlastname').val(), "type" : $('#inputtype').val() }; $.ajax( { method: "post", url: "http://localhost:8080/newjersey/rest/user/filter", data: json.stringify(userfilter), contenttype: "application/json" } ) .done( function(response){ showresults(response); }) .fail( function(jqxhr, textstatus){ alert(textstatus); }); });
and server code:
@post @path("/add") @consumes(mediatype.application_json) public response adduser(user user) throws sqlexception { logger.info("inserting user ... "); try{ dbqueries q = new dbqueries(); int iduser = q.insuserandbankdetails(user); user.setid(iduser); return response.ok("user saved").build(); }catch (sqlexception e){ return response.servererror().entity(e).build(); } }
i got working following @jasonp suggestion.
the problem form submitting data before $.ajax call.
the solution change client as:
$(document).ready(function() { $('#formlookupuser').submit( function (event) { clearresults(); var userfilter = { "email" : $('#inputemail').val(), "firstname" :$('#inputfirstname').val(), "lastname" : $('#inputlastname').val(), "type" : $('#inputtype').val() }; $.ajax( { method: "post", url: "http://localhost:8080/newjersey/rest/user/filter", data: json.stringify(userfilter), contenttype: "application/json" } ) .done( function(response){ showresults(response); }) .fail( function(jqxhr, textstatus){ alert(textstatus); }); event.preventdefault(); // <--- prevent default action. });
Comments
Post a Comment