Sending form data to restful web service @POST method using jquery and ajax -
i have looked everywhere hours , cannot find solution work me. have simple html form gets 3 form inputs , needs send data @post method of web service. after creation, web service show updated list. can tell me doing wrong?
poll.html
<script> function myfunction() { data = $(this).serialize(); $.ajax({ url: 'http://localhost:8080/places3/resourcesc/create', method: 'post', datatype: 'text', data: data, success: function() { alert("worked"); }, error: function(error) { alert("error"); } }); } </script> </head> <body> <form onsubmit="myfunction()"> <p>city: <input type="text" name="city" /> </p> <p>poi1: <input type="text" name="poi1" /> </p> <p>poi2: <input type="text" name="poi2" /> </p> <input type="submit" value="submit" /> </form>
@post method of web service
@post @produces({mediatype.text_plain}) @path("/create") public response create(@formparam("city") string city, @formparam("poi1") string poi1, @formparam("poi2") string poi2) { checkcontext(); string msg = null; // require both properties create. if (city == null || poi1 == null || poi2 == null) { msg = "property 'city' or 'poi1' or 'poi2' missing.\n"; return response.status(response.status.bad_request). entity(msg). type(mediatype.text_plain). build(); } // otherwise, create place , add collection. int id = addplace(city, poi1, poi2); msg = "place " + id + " created: (city = " + city + " poi1 = " + poi1 + " poi2 = " + poi2 + ").\n"; return response.ok(msg, "text/plain").build(); }
i try giving form name such myform in jquery like:
var data = $("#myform").serializearray();
then pass data var in ajax command.
this works me.
Comments
Post a Comment