Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - pass object via post

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.
like image 555
Hailwood Avatar asked Nov 23 '10 12:11

Hailwood


People also ask

How do you post an object in JavaScript?

To post data in JSON format using JavaScript/jQuery, you need to stringify your JavaScript object using the JSON. stringify() method and provide a Content-Type: application/json header with your request.

Does JavaScript pass object by reference?

In JavaScript array and Object follows pass by reference property. In Pass by reference, parameters passed as an arguments does not create its own copy, it refers to the original value so changes made inside function affect the original value.


1 Answers

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));
like image 55
Matt Avatar answered Sep 25 '22 05:09

Matt