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();
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With