> I have an object created using json_encode and via the console.log > entry as suggested, I can see that it is correct. I have a backend > program that I'm wanting to pass this object to, so that it can do a > json_decode and then process that data. When I try to pass the > object to it, using: > $.post(prog,{data:data},function(){},"json"); the result is that > the backend program receives a $_POST['data'] that looks like: > [object Object] instead of the json_encoded string. > > I'm pulling my hair out on this..
I think what's happening is that you're trying to send a full-fledged object along as part of a POST payload, and JavaScript needs to convert it to a string so that it knows how to send it via HTTP. Converting an object to a string doesn't necessarily give you a JSON representation of the object; instead, you get as best a string representation as Firefox can muster--'[object Object]'. That fourth parameter to $.post ('json', the type parameter) indicates how to treat the server *response*, not how to send data to the server. What does your 'data' object look like? Is it simply key => value pairs? You might be able to get away with this: $.post(prog, data, function () { }, 'json'); and PHP will receive one entry in $_POST for each key in your data object. If you do that, the object will be sent as a normal key=value POST payload, rather than as a JSON object bundled in one variable. Since HTTP supports transporting data this way, it makes sense (to me) to take advantage of that. If that doesn't work for you, you'll need to serialize your actual object into its JSON string representation before sending it along to the server; I'm not sure the best method for reliably doing that. I don't *think* jQuery has a method for that, but I wouldn't swear to it. You're looking for something like: http://www.nabble.com/How-to-serialize-an-Object-to-JSON-String-without-making-it-POST--like--td19520848s27240.html with some more discussion here: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=20&t=016362 But like I say, I'm a fan of using HTTP's built-in mechanism (query strings) for transporting structured data *to* a server, and letting the backend server and language deal with figuring it out. HTH