Laravel 5 CORS - XMLHttpRequest 无法加载 http://myapi.com。预检响应具有无效的 HTTP 状态代码 500

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33176021/
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 12:31:04  来源:igfitidea点击:

Laravel 5 CORS - XMLHttpRequest cannot load http://myapi.com. Response for preflight has invalid HTTP status code 500

laravellaravel-5cors

提问by andrepibo

I am using Laravel 5 and a library called CORS. I am facing an error that I don't really understand what it is and why it is happening.

我正在使用 Laravel 5 和一个名为 CORS 的库。我正面临一个错误,我真的不明白它是什么以及它为什么会发生。

I am developing an API that is gonna be used by a website's front-end and an app.

我正在开发一个将由网站的前端和应用程序使用的 API。

I am trying to do an ajax call to the API:

我正在尝试对 API 进行 ajax 调用:

$.ajax({
  url: 'http://myapi.com/call',
  type: 'GET',
  headers: {'Authorization' : 'Bearer token123'},
  crossDomain: true,
  contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  success   : function(data) { console.log(data);},
  error: function(xhr) {console.log(xhr)}
});

However I got this errro:

但是我得到了这个错误:

XMLHttpRequest cannot load http://myapi.com/call. Response for preflight has invalid HTTP status code 500

XMLHttpRequest 无法加载http://myapi.com/call。预检响应具有无效的 HTTP 状态代码 500

However, when I remove the following line from the request works fine.

但是,当我从请求中删除以下行时工作正常。

headers: {'Authorization' : 'Bearer token123'},

EDIT: my config/cors.php

编辑:我的配置/cors.php

'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['*'],
'allowedMethods' => ['GET', 'POST', 'PUT',  'DELETE', 'OPTIONS'],
'exposedHeaders' => [],
'maxAge' => 0,
'hosts' => [],

Any idea about how to solve it?

关于如何解决它的任何想法?

回答by Athul Raj

The following fix worked for me (Laravel 5.4) You can add the following to the laravel cors middleware

以下修复对我有用(Laravel 5.4)您可以将以下内容添加到 Laravel cors 中间件

public function handle($request, Closure $next)
{
     if ($request->getMethod() == "OPTIONS") {
       return response(['OK'], 200)->withHeaders([
           'Access-Control-Allow-Origin' => '*',
           'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE',
           'Access-Control-Allow-Credentials' => true,
           'Access-Control-Allow-Headers' => 'Authorization, Content-Type',
         ]);
     }

     return $next($request)
     ->header('Access-Control-Allow-Origin', '*')
     ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
     ->header('Access-Control-Allow-Credentials', true)
     ->header('Access-Control-Allow-Headers', 'Authorization,Content-Type');
}

and for each of your routes which gets preflight requests add a options method too

并且对于获得预检请求的每条路线也添加一个选项方法

Route::post('/yourroute', 'YourController@index');
Route::options('/yourroute', 'YourController@index');

回答by narendra

Did you configure CORS on server in laravel, there are couple of option to do the same

您是否在 laravel 的服务器上配置了 CORS,有几个选项可以执行相同的操作

# one of the way is to add below in .htaccess (modify accordingly)

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

# other options you can refer to below link of laracasts

Source: https://laracasts.com/discuss/channels/requests/laravel-5-cors-headers-with-filters/?page=2

来源:https: //laracasts.com/discuss/channels/requests/laravel-5-cors-headers-with-filters/?page=2