Sorry for poor english 🙂
I was trying to post form in json format for the restapi written in php/python. I cannot access posted data if i use json . See the scenario below
The code for non json post
jQuery(document).ready(function($) {
$.post(
"http://localhost:8000/api/v1/entry/?format=json",
{
"body": "This will prbbly be my lst edited post.",
"pub_date": "2011-05-22T00:46:38",
"slug": "another-post",
"title": "Another Post",
"username":"admin",
"password":"admin"
},
function(responseText){
$("#result").html(responseText);
},
"html"
);
})
Server response
Array
(
[body] => This will prbbly be my lst edited post.
[pub_date] => 2011-05-22T00:46:38
[slug] => another-post
[title] => Another Post
[username] => admin
[password] => admin
)
Code for Json Post
jQuery(document).ready(function($) {
var data = JSON.stringify({
"body": "This will prbbly be my lst edited post.",
"pub_date": "2011-05-22T00:46:38",
"slug": "another-post",
"title": "Another Post",
"username":"admin",
"password":"admin"
});
$.post(
"testpost.php",
data,
function(responseText){
$("#result").html(responseText);
},
"html"
);
})
Server response
Array
(
)
Well, you havent assigned the value to any parameter, hence PHP won’t be able to populate the
$_POSTarray.Assign it to a parameter, for example,
json:Then you will be able to access it with:
Or if you use PECL, you can call
http_get_request_body[docs] to get the data.