This might seem like an unordinary 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 the json data that is being transferred. I've tried . By the way the request type is . Is there anyway I can obtain the json data into an array or a variable so when I would like to call or I will easily retrieve them from request? 正在传输的json数据进行 。我已经尝试过 。顺便说一句,请求类型是 。无论如何,我都可以将json数据获取到数组或变量,所以当我想调用或 ,可以轻松地从请求中检索它们吗?
I'm following a 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 . 传递的。 Here is the result in Google's Network Tool;
model:{"name":"John","age":10}
If 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.com/backbone/docs/backbone.html#section-162
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.