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

前端 未结 2 1955
走了就别回头了
走了就别回头了 2020-11-28 11:51

My backbone.js application throwing an HTTP OPTIONS not found error when I try to save a model to my restful web service that\'s located on another host/URL.

相关标签:
2条回答
  • 2020-11-28 12:33

    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');
    
    0 讨论(0)
  • 2020-11-28 12:52

    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.

    0 讨论(0)
提交回复
热议问题