HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js codeigniter codeigniter

HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js


I encountered exactly the same problem. To solve it I have a MY_REST_Controller.php in core and all my REST API controllers use it as a base class. I simply added a constructor like this to handle OPTIONS requests.

function __construct() {    header('Access-Control-Allow-Origin: *');    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");    $method = $_SERVER['REQUEST_METHOD'];    if($method == "OPTIONS") {        die();    }    parent::__construct();}

This just checks if the request type is OPTIONS and if so just dies out which return a code 200 for the request.


You can also modify the $allowed_http_methods property in your subclass to exclude the options method. Previous versions of REST_controller did nothing with OPTIONS and adding this line seems to mimic that behavior:

protected $allowed_http_methods = array('get', 'delete', 'post', 'put');