[英]Getting Json Form Data in PHP
This might seem like an unordinary json question but I can assure you I couldn't find an answer to my problem. 这似乎是一个不寻常的json问题,但我可以向您保证,我找不到我的问题的答案。
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? 在下面的代码示例中,我想要获取(可能是对正在传输的json数据进行解码 。我已经尝试过json_decode($ _ POST) 。顺便说一句,请求类型是POST 。无论如何,我都可以将json数据获取到数组或变量,所以当我想调用姓名或年龄时 ,可以轻松地从请求中检索它们吗?
I'm following a backbone.js tutorial and here is my model; 我正在关注一个ribs.js教程,这是我的模型;
<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 . 稍后,我使用Google Chrome浏览器以观察网络,并且可以清楚地看到数据是作为表单数据传递的。 Here is the result in Google's Network Tool; 这是Google网络工具的结果;
model:{"name":"John","age":10}
If you are getting JSON payload as raw post data you can read it with: 如果您将JSON有效负载作为原始发布数据获取,则可以使用以下命令读取它:
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. 如果php.ini设置允许该变量确实被填充,您也可以从$HTTP_RAW_POST_DATA
读取它。
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. 您不能直接将其解码为JSON,因为它不是有效的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. 您需要在$ _POST数组的model
键上执行json_decode。 The model
key contains the json string that you need to decode. model
密钥包含您需要解码的json字符串。
$modelData = json_decode($_POST['model']);
Then you can access the data like $modelData->name
or $modelData->age
; 然后,您可以访问$modelData->name
或$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
. 这样可以使您获得application/x-www-form-urlencoded
而不是application/json
。 This should then populate your $_POST array correctly. 然后,这应正确填充$ _POST数组。
See here for explanation: http://documentcloud.github.com/backbone/docs/backbone.html#section-162 请参阅此处以获取说明: http : //documentcloud.github.com/backbone/docs/backbone.html#section-162
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.