<li>
<form id="byConsumes" class="readJsonForm" action="<c:url value="/mapping/consumes" />" method="post">
<input id="byConsumesSubmit" type="submit" value="By consumes" />
</form>
</li>
$("form.readJsonForm").submit(function() {
var form = $(this); // Variabile che si riferisce all'elemento nel DOM che ha scatenato l'evento click (il form)
var button = form.children(":first"); // Seleziona il bottone submit
var data = form.hasClass("invalid") ? // OPERATORE CONDIZIONALE: il form ha classe "invalid" ?
"{ "foo": "bar" }" : // SI: foo = bar
"{ "foo": "bar", "fruit": "apple" }"; // NO: foo= bar ; fruit = apple
/* AJAX CALL PARAMETER:
type: Say to the servet tath the request is a POST HTTP Request
url: The address to which to send the call
data: the content of my data variable
contentType: an object having JSON format
dataType: the type of content returned by the server
*/
$.ajax({ type: "POST", url: form.attr("action"), data: data, contentType: "application/json", dataType: "text",
success: function(text) { MvcUtil.showSuccessResponse(text, button); },
error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
return false;
});
@RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) {
return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')";
}
JavaBean parameter = new JavaBean();
parameter.setFoo("bar");
parameter.setFruit("apple");