当使用Plesk为React JS和PHP Laravel部署时,它在服务器中给出CORS错误

rjzwgtxy  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(100)

我是一个前端是React JS,后端是Laravel的应用程序。这两个都是使用Plesk部署的。我已经在Laravel后端设置了所有必要的配置来处理CORS问题。但它仍然给CORS错误。
下面是laravel后端的config文件夹中的cors.php文件的内容。

return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => ['*'],
    'allowed_methods' => ['*'],

    'allowed_origins' => ['url_of_react_app'],

    'allowed_origins_patterns' => ['*'],

    'allowed_headers' => ['*'],
    'exposed_headers' => [],
    'max_age' => 0,
    'supports_credentials' => false,

];

它仍然抛出CORS错误。Laravel后端和react应用程序都是通过https进行的。CORS错误是这样的。
CORS策略已阻止从源“URL_of_React_app”访问“URL_of_laravel_backend”处的XMLHttpRequest:对印前检查请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
React app中的API配置是这样的

import axios from "axios";

export const baseURL = "URL_of _laravel_backend";
export const axiosPublicInstance = axios.create({
  baseURL,
  headers: {
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin": "*"
  },
});
wvt8vs2t

wvt8vs2t1#

例如,你可以将url添加到“路径”键中,

<?php 
return [

   'paths' => [
                'api/*', 
                "example1/api/*", 
                "example1/example2/*", 
                "example3/example4/*"
           ],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*'],
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'],
    'exposed_headers' => false,
    'max_age' => false,
    'supports_credentials' => false,  

];

相关问题