我正在尝试使用Vuejs构建一个独立的前端 Web 应用程序,并从我构建的Laravel 9 API中获取数据,当我尝试从前端访问数据(即浏览器控制台中的响应)时:
从源“http://127.0.0.1:8080”访问“http://localhost:8000/api”的 XMLHttpRequest 已被 CORS 策略阻止:“Access-Control-Allow-Origin”标头不存在请求的资源。
1-api.php 文件
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\User;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::get('/', function () {
$users = User::all();
return response()->json($users, 200);
});
Run Code Online (Sandbox Code Playgroud)
2- …