jquery - ajax post request to spring controller -
i trying send form data json jsp file spring controller. this, using jquery ajax post method hit controller. able touch controller , json. in both places, i.e on ajax method call , controller, using post request. still, data gets displayed on url. below files.
jsp page code:
$(document).ready(function(){ $("#submitbutton").click(function(){ var formdata=getformdata(); var strurl="http://localhost:8080/test_reportingui/adddb.htm"; $.post(strurl, {jsondata: json.stringify(formdata)}, function(response){ if(response.status=='ok') { alert("ok"); } else { alert('not ok'); } }, 'json'); }); });
spring controller:
@requestmapping (value="adddb.htm", method=requestmethod.post) modelandview addeditcustomer() { modelandview modelandview=new modelandview("custdb_setup"); system.out.println("hello world.."); return modelandview; }
i want know why getting parameters in above shown url image.
making assumption based on name of submitbutton
element , behaviour you're seeing, sounds need instead hook event submit
event of form
element instead of button can cancel standard form submission occurring ajax request. try this:
$("form").click(function(e){ e.preventdefault(); var formdata = getformdata(); $.post('http://localhost:8080/test_reportingui/adddb.htm', { jsondata: json.stringify(formdata) }, function(response){ if (response.status == 'ok') { alert("ok"); } else { alert('not ok'); } }, 'json'); });
the form
selector example, should change relevant html. note better practice provide formdata
or plain object data
parameter instead of hacking around whatever data you're providing json.stringify
manually. because jquery automatically encode values needed. also, given example, response.status
should boolean. this:
$("form").click(function(e){ e.preventdefault(); var formdata = new formdata(this); $.post('http://localhost:8080/test_reportingui/adddb.htm', formdata, function(response){ if (response.status) { alert("ok"); } else { alert('not ok'); } }, 'json'); });
Comments
Post a Comment