ikk1 ikk1 -5 years ago 162
Javascript Question

Send form elements and array through $.ajax()

I need to pass through $.ajax() method some form elements and an array too. How can i send serialize and array by ajax?

my code bellow:

function loadgraficosajax(){
var arr = ['331234','142323','327767'];

var data = $('#p-form').serialize;

type: "POST",
url: "/page/show",
data: data,
dataType : 'html',
success: function (msg) {
$(document).ajaxComplete(function (event, request, settings) {


Answer Source

serialize is a method in jQuery, not a property, so you should call it in this way:

var data = $('#p-form').serialize();

and to pass your array you need to use param method and modify your array to be inside Object, where array name is object property:

var arr = { arr: ['331234','142323','327767'] };
var data = $('#p-form').serialize();
data += '&' + $.param( arr );

param will transform your object to serialised string:

console.log($.param( arr )); // "arr[]=331234&arr[]=142323&arr[]=327767"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download