简体   繁体   中英

passing data array to $.ajax failed

var array = [];
array.push(something);

var data = {
    action: action,
    array: array
  };

$.ajax({
       type: "POST",
       data: data,
       url: url,
       success: something,
       error: something
    });

In my request in chrome network tab I'm seeing array[] as the property name. That's strange.

It is because of the data processing done by jQuery.

jQuery.ajax()

If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

jQuery.param()

You can pass traditional: true to disable this nature so

$.ajax({
  type: "POST",
  data: data,
  url: url,
  traditional: true,
  success: something,
  error: something
});

Try this:

var myArray = [];
myArray.push(something);

$.ajax({
       type: "POST",
       data: {myArray:myArray},
       url: url,
       success: something,
       error: something
    });

I hope this helps. Cheers.

Try this.This may help you

var array = [];
array.push(something);

      $.ajax({                    
                type : "POST",
                url :  ajax_url,  
                data : {'array' : array},//use single quote for key
                success : function(res){},
                error : function(){}
               });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM