Ad

Unable To Retrieve JSON Parameters By PHP From Backbone

- 1 answer

I save my backbone model with no id therefore it uses the POST protocol to send parameters to my REST API. However I'm unable to catch these parameters, the PUT works just fine and updates the DB but POST method seem to cause problem. One thing I have noticed is that I can catch string parameters but integer parameters are returning as undefined.

my php

if($request_method == 'POST') {
$post_data = file_get_contents("php://input"); 
$post_data = json_decode($post_data, TRUE);
echo $post_data['name'];
}

backbone code

createdata: function(event){
    var newModel = new App.Models.Question({name:$("#_name").val(), surname:$("#_surname").val(), age:$("#_age").val()});
    newModel.save({},{
        success: function (model, response) {
            alert(response.responseText);
            },
            error: function (model, response) {
                alert(response.responseText);
            },      
            wait: true
        }

    );

    console.log(this.myCollection.toJSON());

},
Ad

Answer

It has been a while I have completed this project but still I wanted to post the code that was working fine for me.

createdata: function(event){
            var newModel = new App.Models.Quiz({ 
                name:$("#name").val(), 
                surname:$("#surname").val(), 
                age:$("#age").val()});

            newModel.save({wait:true},{
                success: function (model, response) {
                        $("#blanket").hide();
                        $("#create_popup").hide();
                    },
                    error: function (model, response) {
                        alert(response.responseText);
                    },      
                });
            this.personCollection.add(newModel);
            console.log(this.quizCollection.toJSON());
        },
Ad
source: stackoverflow.com
Ad