laravel 处理 OPTION http 方法请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14414896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:30:17  来源:igfitidea点击:

laravel handling the OPTION http method request

phpcorslaravel

提问by klvmungai

I am developing an angularjs app which uses laravel as its back end server. I am having trouble accessing data from laravel since before each GET request, angular first sends an OPTION request as below

我正在开发一个 angularjs 应用程序,它使用 laravel 作为其后端服务器。我无法从 laravel 访问数据,因为在每个 GET 请求之前,angular 首先发送一个 OPTION 请求,如下所示

OPTIONS /61028/index.php/api/categories HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Access-Control-Request-Method: GET
Origin: http://localhost:3501
Access-Control-Request-Headers: origin, x-requested-with, accept
Accept: */*
Referer: http://localhost:3501/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: UTF-8,*;q=0.5

I have tried to respond this by adding the following code in the before filter

我试图通过在 before 过滤器中添加以下代码来回应这个问题

    if (Request::getMethod() == "OPTIONS") {
     $headers = array(
       'Access-Control-Allow-Origin' =>' *',
       'Access-Control-Allow-Methods'=>' POST, GET, OPTIONS, PUT, DELETE',
       'Access-Control-Allow-Headers'=>'X-Requested-With, content-type',);
      return Response::make('', 200, $headers);
    }

This creates a response with the headers

这将创建一个带有标题的响应

Content-Encoding: gzip
X-Powered-By: PHP/5.3.5-1ubuntu7.11
Connection: Keep-Alive
Content-Length: 20
Keep-Alive: timeout=15, max=97
Server: Apache/2.2.17 (Ubuntu)
Vary: Accept-Encoding
access-control-allow-methods: POST, GET, OPTIONS, PUT, DELETE
Content-Type: text/html; charset=UTF-8
access-control-allow-origin: *
cache-control: no-cache
access-control-allow-headers: X-Requested-With, content-type

Although the headers are set, the browser still throws an error

虽然设置了headers,浏览器还是报错

XMLHttpRequest cannot load http://localhost/61028/index.php/api/categories. Origin http://localhost:3501 is not allowed by Access-Control-Allow-Origin.

I have also tried setting the allow origin to the origin presented in the request header as below

我还尝试将允许原点设置为请求标头中显示的原点,如下所示

    $origin=Request::header('origin');
    //then within the headers
    'Access-Control-Allow-Origin' =>' '.$origin[0],

and still the same error What am i doing wrong? Any help is greatly appreciated.

仍然是同样的错误我做错了什么?任何帮助是极大的赞赏。

Edit 1

编辑 1

I am currently using a very ugly hack where i over-ride laravels initialization when an OPTIONS request is received. This i have done in the index.php

我目前正在使用一个非常丑陋的 hack,当收到 OPTIONS 请求时,我会覆盖 laravel 的初始化。这是我在 index.php 中完成的

<?php
if ($_SERVER['REQUEST_METHOD']=='OPTIONS') {
header('Access-Control-Allow-Origin : *');
header('Access-Control-Allow-Methods : POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers : X-Requested-With, content-type');
}else{
/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @version  3.2.13
 * @author   Taylor Otwell <[email protected]>
 * @link     http://laravel.com
 */

I also had to add the allow-origin header to the before filter.

我还必须将 allow-origin 标头添加到 before 过滤器中。

I know this is not smart but it is my only solution for now

我知道这不聪明,但这是我目前唯一的解决方案

采纳答案by Koushik Samanta

This is with reference to your question regarding the above issue. You did not mention the version of laravel and angularJS. I assume you are using lattest angularJS and Laravel. I also assume, the angular is hosted on http://localhost:3501and the laravel is hosted on http://localhostJust follow the below steps.

这是参考您关于上述问题的问题。你没有提到laravel和angularJS的版本。我假设您使用的是最新的 angularJS 和 Laravel。我还假设,angular 托管在http://localhost:3501 上,laravel 托管在http://localhost 上,只需按照以下步骤操作即可。

  • Put below code block in /public/.htaccess file of laravel

    Header set Access-Control-Allow-Origin "http://localhost:3501"
    Header set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"
    Header set Access-Control-Allow-Credentials "true"
    
  • Put below line in config of angular

    $httpProvider.defaults.withCredentials = true;
    
  • 将下面的代码块放在laravel的/public/.htaccess文件中

    Header set Access-Control-Allow-Origin "http://localhost:3501"
    Header set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"
    Header set Access-Control-Allow-Credentials "true"
    
  • 将下面的行放在 angular 的配置中

    $httpProvider.defaults.withCredentials = true;
    

Never use *as wild card character. Larvel can not identify the domain for session management. So set http://localhost:3501as full domain name to Access-Control-Allow-Origin. I think these will help you.

切勿使用*作为通配符。Larvel 无法识别会话管理的域。因此,将http://localhost:3501 设置Access-Control-Allow-Origin 的完整域名。我想这些会对你有所帮助。

回答by NARKOZ

It's a bug in Laravel that got recently fixed. You may want to update to the latest revision.

这是 Laravel 中最近修复的错误。您可能希望更新到最新版本。

Also, you need to enable CORS support for your server.

此外,您需要为您的服务器启用 CORS 支持。

回答by james kandau

add this in your index.php file

将此添加到您的 index.php 文件中

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');