与AngularJS POST请求失败,preflight OPTION状态code = 404 codeIgniter ResetServer [英] POST request with AngularJS fails with preflight OPTION status code = 404 with CodeIgniter ResetServer

查看:297
本文介绍了与AngularJS POST请求失败,preflight OPTION状态code = 404 codeIgniter ResetServer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的前端应用程序在咕噜运行端口实时服务器 9100 ,而我的PHP服务器端口 80 。主机是一样的,只是不同的端口

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.

当我发送一个 POST 要求 http://dev.site.dev/api/gist 与一些 JSON 数据,我上了preflight错误 404 选项请求。

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.

我已经添加了 CORS 在Apache的配置头:

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.

我应该添加一个 index_option()方法在我的要点控制器?或者问题是别的地方?

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

推荐答案

正如我在答案说明在 codeIgniter bug跟踪系统对于这种问题#313 ,有几种解决方案。

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

我发现从 HTTP OPTIONS错误菲尔鲟鱼的codeigniter解决Restserver和Backbone.js的,这是从价值的名单中删除 otpions $ allowed_http_methods

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');

资源的集中

另一种解决方案是简单的实施 index_options()

Resource's focused

Another solution is to simply implement the index_options().

这并没有为我第一次因错字工作(它的 选项是复数) 。而与此解决方案不再需要使用应用程序/库/ REST_Controller.php 来脾气:

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);
}

现在的preflight OPTION 所以 POST 请求被发送和一切工作的要求永远是正确的: )

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

这篇关于与AngularJS POST请求失败,preflight OPTION状态code = 404 codeIgniter ResetServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆