CORS 错误 405 方法不允许预检响应没有 HTTP 正常状态

CORS Error 405 method not allowed with response for preflight does not have HTTP ok status

我一直在尝试使用 Vue 中的 axios 调用我在 lumen 中的 API,但由于 CORS,它发送选项方法代替 post。

这是我的请求详情

一般

Request URL: http://192.168.1.100/lv_api/code/login

Request Method: OPTIONS

Status Code: 405 Method Not Allowed

Remote Address: 192.168.1.100:80

Referrer Policy: no-referrer-when-downgrade

Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token, X-
Requested-With

Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS

Access-Control-Allow-Origin: http://192.168.1.100:8000

Allow: POST

Cache-Control: no-cache, private

Connection: close

Content-Type: text/html; charset=UTF-8

Date: Wed, 03 Oct 2018 06:22:56 GMT

Server: Apache/2.4.18 (Ubuntu)

Accept: */*

Accept-Encoding: gzip, deflate

Accept-Language: en-GB,en-US;q=0.9,en;q=0.8

Access-Control-Request-Headers: content-type

Access-Control-Request-Method: POST

Connection: keep-alive

Host: 192.168.1.100

Origin: http://192.168.1.100:8000

User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36

已尝试设置 headers,但我不确定要为该问题设置的正确值。

如果我在 API 中为 Options 请求创建一个方法,它可以正常工作,但这不是正确的解决方案。

提前致谢。

通过在我的 api 代码中添加以下行解决了这个问题

header('Access-Control-Allow-Origin: your origin');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age:86400');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token,  Accept, Authorization, X-Requested-With');

您可以安装 spatie/laravel-cors 来解决这个问题