POST request with AngularJS fails with preflight OPTION status code = 404 with CodeIgniter ResetServer

荒凉一梦 提交于 2019-12-05 06:43:00

问题


My front-end application is running on a grunt live server on port 9100, while my PHP server is on the port 80. The host is the same, just the port differ.

When I send a POST request to http://dev.site.dev/api/gist with some JSON data, I got an error 404 on the preflight OPTIONS request.

I already added the CORS headers in apache configuration:

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"

``` and restart the server but still got the issue.

Should I add an index_option() method in my gist controller ? Or the problem is somewhere else ?


回答1:


As I described in my answer on the CodeIgniter bug tracker for this "issue" #313, there is several solutions.

Application wide

I found a solution from HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js, which is to remove otpions from the list of value in $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');

Resource's focused

Another solution is to simply implement the index_options().

It didn't work for me the first time due to a typo (it's OPTIONS is plural ). And with this solution no more need to temper with applications/libraries/REST_Controller.php:

public function index_options() {
    return $this->response(NULL, 200);
}

Now the preflight OPTION request is always true so the POST request is sent and everything works :)




回答2:


Yes you have to add the index_options() method.

I had the same problem and it only worked when i added the OPTIONS method with the same arguments as my POST method.




回答3:


in my case it was a routing problem.

What I did is overide the 404 routing. After that, the request got through the routing and the rest server did all the rest.

This is what I put in my routes.php:

$route['404_override'] = 'auth/options';


来源:https://stackoverflow.com/questions/23954433/post-request-with-angularjs-fails-with-preflight-option-status-code-404-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!