Backbone Sync returns an empty $ _POST array

I am trying to make my first RESTful application with Backbone and Yii Framework. I had no problems with the GET methods, but I am now stuck with the POST method to create a new element.

I have a comment model in Backbone:

var commentModel = Backbone.Model.extend({

    urlRoot: "index.php/api/comments",
    idAttribute: 'id',

    defaults: {
        content: "Empty comment",
        status: 1
    }
});

In my opinion, I am adding a function to create a new comment passing values ​​from a relative form:

on_submit: function(e) {
            var new_comment = new Comment({author_id: this.$('#author_text').val(), content: this.$('#content_text').val(), post_id: this.$("#post_text").val(), status: this.$("#status_text").val()});

         new_comment.save();
        },

Searching for a request using Firebug seems correct, on the POST tab I see all the values:

JSON            
author_id "7"   
content "Epic fail"
post_id "7" 
status "2"

Source 
{"content":"Epic fail","status":"2","author_id":"7","post_id":"7"}

But in my php Api $ _POST var is empty!

foreach($_POST as $var=>$value) {

     if($model->hasAttribute($var))
        $model->$var = $value;
     else
        $this->_sendResponse(500);
}

Does anyone have any ideas? Reading Backbone.Sync documentation I understand that you should use POST to create a request.

I found a workaround getting values ​​from:

file_get_contents('php://input') 

but id i don't like ...

Thank.

+5
source share
1 answer

Backbone.sync,

, Backbone.sync , , JSON, HTTP /json.

, , , HTTP.

:

+10

All Articles