【发布时间】:2023-02-04 09:00:38
【问题描述】:
我是 Laravel 的新手,我正在使用 Laravel 作为我的 Angular 项目的 API。
使用get 方法检索数据时使用 Laravel 没有问题,但是使用 post 方法向其中插入数据时遇到大问题
我有以下错误:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
我发誓这是我在 api.php 的代码
<?php
use App\Models\sample;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Resources\SampleResource;
use App\Http\Controllers\SampleController;
/*
|--------------------------------------------------------------------------
| 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::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::match(['get', 'post'], '/', function () {
//
});
//I tried to add this hoping it will work, it wont
Route::post('/sample', [SampleController::class, 'store']);
// Route::post('/sample', function(Request $request){
// return sample::create($request->all);
// });
【问题讨论】:
-
你在哪个 api 中得到这个错误你能提到吗
-
我假设您来自 angular 的请求具有
/api/路径,对吗?/api/sample