dtuct88226 2016-01-01 03:53
浏览 49
已采纳

无法从骨干网检索PHP的JSON参数

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());

},
  • 写回答

1条回答 默认 最新

  • duanqun9618 2016-02-17 06:33
    关注

    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());
            },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?