Search code examples
phpbackbone.jsheaderserverresponse

PHP Server Response - Backbone save not triggering success function


I can't figure out how to create a correct server response after calling Backbone.Model.save().

Consider myModel to be an initialized Backbone Model instance.

myModel.set('foo', '123');
myModel.save().then( function() {
    console.log('saved foo');
}, function() {
    console.log('error');
} );

The console output is 'error' allways. The new value for 'foo' is set on the server though and after reloading the app the view representing myModel.get('foo') will show the expected value ('123'). However the promise never evaluates to the success function.

As my server is very simple I am almost certain that I am doing something wrong there - which is this

if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
    if( isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) ) {
        if( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] == 'PUT' ) {
            // ... doing stuff to update data on server
            http_response_code(200);
            echo '';
        }
    }
}

This is the received response header

Content-Length 0
Content-Type   text/html; charset=UTF-8
Server         PhpStorm 9.0.1
X-Powered-By   PHP/5.6.12

I very much appreciate every hint on what I am missing here.


Solution

  • header() is used to set HTTP-headers,

    So add the line of code

    header("Content-type: application/json"); 
    $rtn = array("status", "200");
    http_response_code(200);
    echo json_encode($rtn);