This might seem like an unordinary json question but I can assure you I couldn't find an answer to my problem.
In the code example below I would like to get (possibly decode the json data that is being transferred. I've tried json_decode($_POST). By the way the request type is POST. Is there anyway I can obtain the json data into an array or a variable so when I would like to call name or age I will easily retrieve them from request?
I'm following a backbone.js tutorial and here is my model;
<script type="text/javascript">
var URL=Backbone.Model.extend({
initialize: function()
{
console.log("URL has been initialized");
},
defaults:{
name:'not defined',
age:'not defined'
},
urlRoot:"/Backbone/manage.php",
url:function(){
var base = this.urlRoot || (this.collection && this.collection.url) || "/";
console.log("Base has been produced");
if(this.isNew()) return base;
return base+"?id="+encodeURIComponent(this.id);
}
});
var url=new URL({name:"John",age:10});
url.save();
</script>
Later on I use Google Chrome in order to watch the network and I can clear see that the data is passed as form data. Here is the result in Google's Network Tool;
model:{"name":"John","age":10}
This might seem like an unordinary json question but I can assure you I couldn't find an answer to my problem.
In the code example below I would like to get (possibly decode the json data that is being transferred. I've tried json_decode($_POST). By the way the request type is POST. Is there anyway I can obtain the json data into an array or a variable so when I would like to call name or age I will easily retrieve them from request?
I'm following a backbone.js tutorial and here is my model;
<script type="text/javascript">
var URL=Backbone.Model.extend({
initialize: function()
{
console.log("URL has been initialized");
},
defaults:{
name:'not defined',
age:'not defined'
},
urlRoot:"/Backbone/manage.php",
url:function(){
var base = this.urlRoot || (this.collection && this.collection.url) || "/";
console.log("Base has been produced");
if(this.isNew()) return base;
return base+"?id="+encodeURIComponent(this.id);
}
});
var url=new URL({name:"John",age:10});
url.save();
</script>
Later on I use Google Chrome in order to watch the network and I can clear see that the data is passed as form data. Here is the result in Google's Network Tool;
model:{"name":"John","age":10}
Share
Improve this question
asked May 17, 2012 at 22:48
AliAli
5,47612 gold badges58 silver badges79 bronze badges
1
-
Use
print_r($_POST);
to see which field name carries the payload, if any. It doesn't just show up as$_POST
with raw data. It would bejson_decode($_POST["varname"])
(e.g."id"
, but that's your issue) or possibly reside inphp://input
. – mario Commented May 17, 2012 at 22:53
2 Answers
Reset to default 2If you are getting JSON payload as raw post data you can read it with:
json_decode(file_get_contents('php://input'));
You could also read it from $HTTP_RAW_POST_DATA
if php.ini settings allow for that variable do be filled.
Since what you are getting is:
model=%7B%22name%22%3A%22John%22%2C%22age%22%3A10%7D
You can't directly decode it as JSON because it's not valid JSON.
So try:
parse_str(file_get_contents('php://input'), $post);
$myObject = json_decode($post['model']);
You'll need to do json_decode on the model
key of the $_POST array. The model
key contains the json string that you need to decode.
$modelData = json_decode($_POST['model']);
Then you can access the data like $modelData->name
or $modelData->age
;
EDIT:
Try adding this to your backbone set up:
Backbone.emulateHTTP = true;
Backbone.emulateJSON = true;
This will make it so you are getting application/x-www-form-urlencoded
instead of application/json
. This should then populate your $_POST array correctly.
See here for explanation: http://documentcloud.github./backbone/docs/backbone.html#section-162