I would like to implement an ajax call by sending data in JSON format . Something wrong in the syntax example :
var string_json='{"usr":{"name":"john","surname":"do"}}';
$.ajax({ url:"myurl",type:"post",data:{user:JSON.stringify(string_json)},
success:function(info){
alert(info); }
})
PHP side:
$var=$_POST['user'];
$user=json_decode($var);
echo $user->usr->name ;
at the end of the call http, I see nothing in the message alert.
string_json
is already in JSON format, so you could do:
data: {user: string_json}
However, it's better to call JSON.stringify
on the original object, rather than construct the JSON string by hand.
var user = { usr: { name: "john", surname: "do" } };
$.ajax({
url: "myurl",
type: "post",
data: { user: JSON.stringify(user) }
success: function(info) {
alert(info);
}
});
data needs to be a string, so call JSON.stringify on whatever you set data to.
var usr : {
name : "john",
surname : "do"
};
$.ajax({ url: "myurl", type:"post", data: JSON.stringify({ user: usr }),
success: function(info){
alert(info);
}
});