Im just doing a simple POST, but for some reason no matter what I do, I cant read the JSON data
$.ajax({
url: 'server/account/session',
type: 'POST',
data: { "username": name, "password": password} ,
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (data) {
sessionStorage.accessToken = data["token"];
sessionStorage.userid = data["userid"];
alert ( sessionStorage.accessToken );
},
error: function () {
}
});
data: {"username": "chips", "password": "potato"}
my php:
$result = json_decode($app->request->getBody(), true);
var_dump($result);
would result in NULL. I don't get why it will return NULL, I sent a json then decode it in php into a. array, isnt that what Im doing?
QUESTION TWO: 2) A related question:
if I were to send through jquery.post
jQuery.post("server/account/session", { "username": name, "password": password}, function(data){
var result = JSON.parse(data);
sessionStorage.accessToken = result["token"];
sessionStorage.userid = result["userid"];
alert ( sessionStorage.accessToken );
});
and recieve it in php by
$userid = $app->request->params('username');
it works perfectly, but my question is, I thought when we do POST, the data is sent through the Body instead of header. How is it that when I do jquery.post, i can recieve it by app's request->params??
EDIT:
OK I fixed the problem. Turn out even if you specify content type to be Json. It doesnt mean it will convert the data to JSON. I had to do JSON.stringify on the data.
But can anyone help me on the second question?