javascript - Is it possible for a Java controller to pass an attribute to a success ajax call? -
as title says, want pass variable controller success ajax call function possible? example:
i have java class controller this:
@requestmapping(value = "checkfruit", method = requestmethod.post) @responsebody public modelandview checkfruit(httpservletrequest request, string fruitinput) { modelandview modelandview = new modelandview(); if (fruitinput.equals("apple")) { modelandview.addobject("fruittype", 1); //this want pass success ajax call. } else if (fruitinput.equals("orange") { modelandview.addobject("fruittype", 2); //this want pass success ajax call. } modelandview.setviewname("fruitgarden"); return modelandview; }
and jsp view ajax call this:
$("#checkfruitbtn").click(function () { var fruitinput = $("input[name=fruitinput]").val(); $.ajax({ type: 'post', url: 'checkfruit', data: 'fruitinput=' + fruitinput, success: function () { var fruittype= ${fruittype}; //i want attribule of "fruittype" variable set in controller. things went wrong. if (fruittype == 1) { alert("this apple."); } else if (fruittype == 2) { alert("this orange."); } window.location.href = "/someurl"; } }); });
in above example. when click button "checkfruitbtn", send ajax call controller "checkfruit". , in controller, i'll set variable "fruittype" send success ajax call function. in success ajax call function, i'll value of "fruittype" variable check , show alert message according value... but, things aren't going planned.
my question is, there possible way of getting value of "fruittype" variable? i've searched getting method still can't find 1 fitted in case. i'm terribly sorry if question have been asked before.
thanks in advance!
with annotation @responsebody, springmvc converts returned object response body using httpmessageconverter.
modelandview involves command object , view name.
so can use modelandview jsp page , use el evaluate command object values. use @responsebody, springmvc return strings.
Comments
Post a Comment