1.Serialize форма к JSON-Object
$.fn.serializeObject = function(){
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
2.Define запрос АЯКС с типом контента application/json
$.ajaxSetup({
contentType: "application/json; charset=utf-8"
});
function request(path, params, method) {
method = method || "POST";
$.ajax({
url: path,
type: method,
data: params,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
//do something
},
error: function (xhr, ajaxOptions, thrownError) {
//do something
}
});
}
3.Send данных после отправки формы
$(function() {
var url = "/api/route";
$('form').submit(function() {
var json = JSON.stringify($('form').serializeObject());
request(url, json);
return false;
});
});
Вам действительно нужен json в контроллере? Или будет POJO в порядке? –
Я предпочитаю JSON .. –
Вы просмотрели https://www.playframework.com/documentation/2.3.x/JavaJsonActions –