2

I have a jQuery array and I want to use it in an AJAX call with my PHP script, can anyone help me do this?

var things = {
  "foo": ["bar", "baz"],
  "bar": 1234
};

var output = "?foo[]=bar&foo[]=baz&bar=1234";

Thanks!!

user2864740
  • 60,010
  • 15
  • 145
  • 220
tarnfeld
  • 25,992
  • 41
  • 111
  • 146

2 Answers2

5

$.param(myobj, true)

From the manual:

As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;.

As of jQuery 1.8, the $.param() method no longer uses jQuery.ajaxSettings.traditional as its default setting and will default to false. For best compatibility across versions, call $.param() with an explicit value for the second argument and do not use defaults.

Community
  • 1
  • 1
Ross Deane
  • 3,100
  • 2
  • 19
  • 25
2

$.param(things, true)

The above will help you here. The second parameter allows for arrays. Make sure you have jQuery installed

andy
  • 2,369
  • 2
  • 31
  • 50