I am trying to send following javascript object using AJAX
var bodycontent = {
search: search,
query: query,
start: start,
show: show,
};
My AJAX call
$.ajax({
type: "POST",
data: dataString,
cache: false,
url: 'url.php',
success: function(html) {
alert(html);
}
})
The variable dataString
var dataString = "bodycontent="+bodycontent;
I tried console.log for bodycontent to ensure it had data and the result is as follows
{search: "human", query: Array(1), start: 20, show: 40}
On PHP page I tried follow code to decode the object being sent
json_decode($_POST['bodycontent'],true)
But the value is empty. If I don't json_decode it returns [object Object]
JSON.stringifyto convert that Object literal to a string?data: JSON.stringify([dataString ])datato be a live object, a string or an array. You're posting this string: "bodycontent=[object Object]", and that's not valid JSON, which your server expects to get.