admin管理员组文章数量:1391929
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
本文标签: javascriptGetting Json Form Data in PHPStack Overflow
版权声明:本文标题:javascript - Getting Json Form Data in PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744731029a2622048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论