使用CodeIgniter ResetServer,使用AngularJS的POST请求失败,预检OPTION状态代码= 404

Édo*_*pez 1 codeigniter http-post cors angularjs codeigniter-restserver

我的前端应用程序grunt在端口上的实时服务器上运行9100,而我的PHP服务器在端口上80.主机是相同的,只是端口不同.

当我向某些数据发送POST请求时,我在预检请求上收到错误.http://dev.site.dev/api/gistJSON404OPTIONS

我已经CORS在apache配置中添加了标头:

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "X-Requested-With, accept, content-type"
Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Run Code Online (Sandbox Code Playgroud)

``并重新启动服务器但仍然遇到问题.

我应该index_option()gist控制器中添加方法吗?或问题出在其他地方?

Édo*_*pez 8

正如我在关于这个"问题"#313 CodeIgniter错误跟踪器的回答中所描述的,有几种解决方案.

适用范围广

在Phil Sturgeon的Codeigniter Restserver和Backbone.js中找到了来自HTTP OPTIONS错误的解决方案,该解决方案将从以下otpions值中删除$allowed_http_methods:

// protected $allowed_http_methods = array('get', 'delete', 'post', 'put', 'options', 'patch', 'head');
   protected $allowed_http_methods = array('get', 'delete', 'post', 'put', 'patch', 'head');
Run Code Online (Sandbox Code Playgroud)

资源集中

另一种解决方案是简单地实现index_options().

由于拼写错误,它第一次对我不起作用(它OPTIONS是复数).使用此解决方案不再需要调整applications/libraries/REST_Controller.php:

public function index_options() {
    return $this->response(NULL, 200);
}
Run Code Online (Sandbox Code Playgroud)

现在预检OPTION请求始终为真,因此POST发送请求并且一切正常:)