2016. 2. 17. 15:42

html form을 json으로 변환

serializeObject function 추가
$.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;
};


호출

$.ajax({
	url : 'url',
	contentType: 'application/json',
	data : JSON.stringify($("form").serializeObject()),
	type : 'POST',
	dataType : 'json',
	success : function(result) {
		.....
	}
});


[참고] Send JSON Data on HTML form submit

Trackback 0 Comment 1
  1. 2016.06.13 11:49 address edit & del reply

    비밀댓글입니다