5

I'm trying to do my first RESTful app with Backbone and Yii Framework. I had no problem with the GET methods but I'm stuck now 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 view I add a function to create a new Comment passing the values from the 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();
        },

Looking the request with Firebug it seems all right, in the POST tab I can 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 the $_POST var is empty!

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

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

Anyone has some ideas? Reading the documentation of Backbone.Sync I understand that it should use POST for create request.

I found a workaround getting the values from:

file_get_contents('php://input') 

but id doesn't feel right to me...

Thanks.

3
  • 1
    Something like that stackoverflow.com/questions/10930789/… ? Commented Aug 2, 2012 at 9:19
  • The $_SERVER['REQUEST_METHOD'] value is "POST" so the values should be inside the $_POST array, I'm right? Commented Aug 2, 2012 at 9:39
  • No, the method is a POST, but the data is sent the HTTP body. It is not sent as a regular form would be with parameters set and a application/x-www-form-urlencoded as mime type. Check what is sent in this Fiddle jsfiddle.net/RK7LN Commented Aug 2, 2012 at 9:54

1 Answer 1

10

From Backbone.sync documentation,

With the default implementation, when Backbone.sync sends up a request to save a model, its attributes will be passed, serialized as JSON, and sent in the HTTP body with content-type application/json.

which means you won't receive data in parameters like in a regular form post but in the body of the HTTP response.

You've got three options to deal with this behavior:

  1. Modify your server code to understand REST requests, see Insert Backbone.js model into MySQL database for example
  2. See if Backbone.emulateJSON could answer your needs
  3. Overwrite Backbone.sync and send the data to better fit your server
Sign up to request clarification or add additional context in comments.

1 Comment

Well it seems that I made a wrong assumption. Thanks for the clarification, now I'll make some test and see what solution will fit better my app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.