Phil Sturgeon 的 Codeigniter Restserver 和 Backbone.js 中的 HTTP OPTIONS 错误 [英] HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js

查看:18
本文介绍了Phil Sturgeon 的 Codeigniter Restserver 和 Backbone.js 中的 HTTP OPTIONS 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将模型保存到位于另一个主机/URL 上的 Restful Web 服务时,我的 backbone.js 应用程序抛出一个 HTTP OPTIONS not found 错误.

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.

显然 CORS 的请求会对用户数据产生副作用"将使您的浏览器预检"在实际发送您想要的 HTTP 请求方法之前,带有 OPTIONS 请求标头的请求以检查批准.

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:

  • 将 Backbone 中的 emulateHTTP 设置为 true.

Backbone.emulateHTTP = true;

Backbone.emulateHTTP = true;

  • 我还允许在标头中允许所有 CORS 和 CSRF 选项.

    • I also allowed allowed all CORS and CSRF options in the header.

      header('Access-Control-Allow-Origin: *');
      标头(访问控制允许标头:来源,X-Requested-With,Content-Type,Accept");标头(访问控制允许方法:GET、POST、OPTIONS");

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

      当引入 Backbone.emulateHTTP 代码行时,应用程序崩溃了.

      The application crashed when the Backbone.emulateHTTP line of code was introduced.

      有没有办法响应 CodeIgniter RESTServer 中的 OPTIONS 请求,是否还有其他替代方法可以允许从谈话场所禁用此请求?

      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 上发现 this 作为一种解决方案.我不确定是否应该使用它,因为它看起来有点过时了.

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

      这只是检查请求类型是否为 OPTIONS,如果是,则为请求返回代码 200.

      This just checks if the request type is OPTIONS and if so just dies out which return a code 200 for the request.

      这篇关于Phil Sturgeon 的 Codeigniter Restserver 和 Backbone.js 中的 HTTP OPTIONS 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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