2

I have a backbone script that calls a Slim REST service. GET requests are working fine, PUT requests are returning 404 Not Found. Note: this was working until my code was recently moved to a different server (and it works locally), so I'm guessing it has something to do with an Apache config setting. Here's a snippet of the backbone script:

jQuery(document).ready(function ($) {

    //define box model
    var Box = Backbone.Model.extend({
        url: function () {
            var urlId = (this.id) ? this.id : "";
            var myUrl = "/wp-includes/api/service.php/box/" + urlId;
            return myUrl;   
        }
    });

        var BoxView = Backbone.View.extend({
        tagName: "div",
        template: $("#boxTemplate").html(),

        initialize: function () {
            this.model = new Box(box);
            this.render();
        },

        saveBox: function(e){
            e.preventDefault();
            $("#boxMessage").empty();
            var formData = {},
                prev = this.model.previousAttributes();

            $(e.target).closest("form").find(":input").not("button").each(function (){          
                var el = $(this);
                formData[el.attr("id")] = el.val();
            });

            this.model.set(formData);
            this.model.save(
                { },
                {
                    success: function() {
                        $("#boxMessage").html("Box information saved.");
                    },
                    error: function() {

                    }
                }
            );
        }

Here's a snippet of the Slim REST service:

<?php

require 'Slim/Slim.php';

$app = new Slim();

$app->get('/workouts/:id',  'getWorkout');
$app->put('/box/:id', 'updateEventBox');

$app->run();

function getWorkout($id) {
    echo json_encode(GetEventCompetitorWorkout($id));
}

function updateEventBox($id) {
    $request = Slim::getInstance()->request();
    $body = $request->getBody();
    $eventBox = new EventBox(null);
    $eventBox->TakeJson($body);
    $eventBox->Save();
}

And here's the header info for the request:

Request URL:http://www.mydomain.com/wp-includes/api/service.php/box/1
Request Method:PUT
Status Code:404 Not Found

UPDATE: just tested a POST to the same service and it worked fine. PUT still fails.

james28x
  • 47
  • 1
  • 8

1 Answers1

3

I've found it not too uncommon that some servers only have GET,POST,HEAD enabled and that PUT,DELETE which are needed for REST are not enabled. That could be the case.

To test for this you can tell Backbone to use "emulatated HTTP" by calling this before your code:

Backbone.emulateHTTP = true;

This will make backbone use only GET/POST requests however it will append to the querystring "_method=PUT" or "_method=DELETE" which you can use on the serverside to detect the intended HTTP verb.

So on the server side you need to do something like this like as the first line of your index.php file (before any of the framework loads):

if (isset($_REQUEST['_method'])) $_SERVER['REQUEST_METHOD'] = $_REQUEST['_method'];
Jason
  • 1,726
  • 17
  • 19