Similarly to the $.post API I do:
$.post('http://...', POST_BODY, function(data) {...});
POST_BODY GIVES ME
{name:'test'} Empty response
{'name':'test'} Empty response
{"name":"test"} Empty response
'{"name":"test"}' stdClass Object([name] => test)
JSON.stringify({"name":"test"}) stdClass Object([name] => test)
e.g.
$.post('http://...', {name:'test'}, function(data) {...});
My php rest function:
function postContact(...) {
$request = Slim::getInstance()->request();
$body= json_decode($request->getBody());
print_r($body);
}
Why can’t I use object literal as described in jQuery API? Is it my server-side or client-side which is wrong?
You understood this wrong. Passing an object literal will cause jQuery to take each property of the object as a key and turn all values into strings and transform this into post parameters, which have the form
key1=val1&key2=val2. And of course that’s no JSON and thusjson_decodefails silently.