此路由不支持GET方法.支持的方法:POST.但我的方法是张贴



我是Laravel的新手,我正在使用Laravel作为Angular项目的API。当我在使用get方法检索数据时使用Laravel时,我没有问题,但当使用post方法将数据插入其中时,我有一个大问题

我有以下错误:

SymfonyComponentHttpKernelExceptionMethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.

我发誓这是我在api.php的代码

<?php

use AppModelssample;
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use AppHttpResourcesSampleResource;
use AppHttpControllersSampleController;

/*
|--------------------------------------------------------------------------
| 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);
// });

我试着按照下面链接中的说明操作,希望它能帮助我,但不会这这个

根据您的问题,此路由不支持GET方法,而您的后端路由是post,因此当您访问此API时,可能不会使用post方法。您必须使用post方法将数据发送到后端。您可以在jquery中遵循以下示例,但必须将其应用于您的角度项目上下文中,如Axios或其他任何内容:

客户端请求=>

$.ajax({
type: "POST",
url: "{{route('check_inventory_product')}}",
data: data,
success: function (response) { 
console.log(response);
}
});

后端路由=>

Route::post('check_inventory_product'[InventoryController::class,'check_inventory_product'])->name('check_inventory_product');

请看这里,我使用了ajax请求类型POST和路由方法类型POST。

好吧,这是答案,伙计们,这个错误根本不是错误。如果这就是所谓的";错误";出现在我的屏幕上,这只意味着我的API或路线正在工作。我试着把这个调用到我的角度项目中,它起作用了!

有时这不是错误感谢大家抽出时间

最新更新