在菲尔鲟鱼的codeigniter Restserver和Backbone.js的HTTP OPTIONS错误 [英] HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js
问题描述
我的 Backbone.js的
应用程序抛出找不到HTTP OPTIONS错误,当我尝试将模型保存到位于第另一台主机/ URL我的RESTful Web服务。
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.
根据我的研究,我从这个帖子会集:
Based on my research, I gathered from this post that :
的请求会不断发送OPTIONS HTTP请求头,并没有触发POST请求的。
a request would constantly send an OPTIONS http request header, and not trigger the POST request at all.
显然与要求,将上的用户数据产生副作用的会让你的浏览器preflight与OPTIONS请求头请求实际发送您预期的HTTP请求方法之前检查审批,CORS。
Apparently CORS with requests that will "cause side-effects on user data" will make your browser "preflight" the request with the OPTIONS request header to check for approval, before actually sending your intended HTTP request method.
我试图来解决这个问题是:
I tried to get around this by:
- 在骨干Settting emulateHTTP为true。
Backbone.emulateHTTP = TRUE;
Backbone.emulateHTTP = true;
-
我也允许允许在头所有CORS和CSRF的选择。
I also allowed allowed all CORS and CSRF options in the header.
标题(访问控制允许来源:*');结果
标题(访问控制允许信头:产地,X-要求-着,内容类型,接受);
标题(访问控制允许的方法:GET,POST,选项);header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); header("Access-Control-Allow-Methods: GET, POST, OPTIONS");引入code的
Backbone.emulateHTTP
线时,应用程序崩溃。The application crashed when the
Backbone.emulateHTTP
line of code was introduced.有没有办法应对选项codeIgniter RESTServer要求以及是否有任何其他的替代品,让无论是从说话的地方禁用此要求?
Is there a way to respond to OPTIONS request in CodeIgniter RESTServer and are there any other alternatives to allow either disable this request from talking place?
我发现这在Github上的溶为一体。我不知道,因为它似乎有点过时,我应该使用它。
I found this on Github as one solution. I am not sure if I should use it as it seems a bit outdated.
推荐答案
我遇到过完全相同的问题。要解决它,我有一个MY_REST_Controller.php核心和我所有的REST API的控制器使用它作为一个基类。我只是增加这样一个构造函数来处理OPTIONS请求。
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(); }
这只是检查是否请求类型的选项,如果是这样刚刚去世哪些返回code 200的请求。
This just checks if the request type is OPTIONS and if so just dies out which return a code 200 for the request.
这篇关于在菲尔鲟鱼的codeigniter Restserver和Backbone.js的HTTP OPTIONS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!