小编log*_*rap的帖子

CORS 问题:Laravel 10 API 请求中不存在“Access-Control-Allow-Origin”标头

当我尝试从 React 应用程序向 Laravel API 发出 GET 请求时,我遇到了 CORS(跨域资源共享)问题。我在控制台中收到的具体错误如下:

从源“http://localhost:19000”访问“https://0748-102-146-2-29.ngrok-free.app/api/products-api”处的 XMLHttpRequest 已被 CORS 策略阻止:否请求的资源上存在“Access-Control-Allow-Origin”标头。

以下是我的设置的相关详细信息:Laravel API 配置(config/cors.php):

return [
'paths' => ['api/*', 'http://localhost:19000', 'sanctum/csrf-cookie'],
'allowed_methods' => ['GET', 'POST', 'PUT', 'DELETE'], // Add other methods as needed
'allowed_origins' => ['*'],
'allowed_headers' => ['*'],
// ...
];
Run Code Online (Sandbox Code Playgroud)

React 应用程序(useProductFetch.js):

import { useState, useEffect } from "react";
import axios from "axios";

const fetchData = async () => {
setIsLoading(true);

try {
  const response = await axios.get(
    "https://0748-102-146-2-29.ngrok- 
    free.app/api/products-api"
  );
  console.log("Axios request successful");

export …
Run Code Online (Sandbox Code Playgroud)

php cors laravel axios

6
推荐指数
1
解决办法
2294
查看次数

标签 统计

axios ×1

cors ×1

laravel ×1

php ×1