14

I have an object that looks like this

var obj = { p1 : true, p2 : true, p3 : false }

I am looking to try and pass this object as part of a post request.

however on the other end (in php) all I get is

[object Object]

How can I send an object via post?

basically what I am trying to do is

I have an input that is hidden and is created like so

<input id="obj" type="hidden" name="obj[]">

which is part of a hidden form.

when a button is pressed I have

$(#obj).val(obj);
$('form').submit();


Please no suggestions to use ajax as I must do it this way as it is to download a dynamically created file.
Hailwood
  • 89,623
  • 107
  • 270
  • 423

2 Answers2

28

You need to serialize/convert the object to a string before submitting it. You can use jQuery.param() for this.

$('#obj').val(jQuery.param(obj));
Matt
  • 74,352
  • 26
  • 153
  • 180
13

You might consider using JSON notation to send the object to the server. If you include a JSON parser/renderer in your page, (it's built in on all modern browsers now, and also IE8 in standards mode) you can convert the object into a string preserving its full object graph. Most server-side languages now have JSON parsing available for them (in PHP it's json_decode, for instance). You can put that string in your hidden form field before sending the form.

That would look like this:

$('#obj').val(JSON.stringify(obj));
$('form').submit();

...and your server-side would see a string in the form

{ "p1" : true, "p2" : true, "p3" : false }
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Please read the whole question. The form is needed as it is downloading a dynamically generated file, You cannot cause a file download from ajax data. I cannot store the file that is created anywhere either so, generating the file via the ajax call and returning a url will not work either. – Hailwood Nov 23 '10 at 12:12
  • @Hailwood: Can't believe I missed that. Fixed. – T.J. Crowder Nov 23 '10 at 12:15
  • @T.J. Cheers, Your new answer will work. However i am using @Matt's answer :) (ps, you get an upvote from me) – Hailwood Nov 23 '10 at 12:17
  • @Hailwood: Glad Matt's solution will work for you, it's all about having options. :-) – T.J. Crowder Nov 23 '10 at 12:18
  • Exactly, Actually I do prefer this option, but I am including jquery already, unless it has a built in JSON parser I dont really want to include another library. Most browsers now have a JSON parser already, but... IE6... – Hailwood Nov 23 '10 at 12:21
  • @Hailwood: jQuery has a JSON *parser* built in (`jQuery.parseJSON`) but you actually want the opposite, the stringifier, which it doesn't. FWIW, json2.js [minified](http://www.crockford.com/javascript/jsmin.html) is 3,408 bytes. If you can include that in another script file you're already including in the page, I doubt it'll make any difference. If you can't, then you have the overhead of the additional HTTP request... Have fun! – T.J. Crowder Nov 23 '10 at 12:30
  • JSON.stringify seems to be a built-in since ECMAScript 5.1 (ECMA-262) https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/JSON/stringify – DestyNova Jul 21 '15 at 18:06
  • @DestyNova: Yes. Back in 2010, though, the standard was brand-new and many widely-used browsers didn't have support built-in yet... – T.J. Crowder Jul 21 '15 at 19:14