【问题标题】:The GET method is not supported for this route. Supported methods: POST. But my method is post [duplicate]此路由不支持 GET 方法。支持的方法:POST。但我的方法是发布[重复]
【发布时间】: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);
// });

我已经尝试按照下面链接中的说明进行操作,希望它能帮助我但不会 this this

【问题讨论】:

  • 你在哪个 api 中得到这个错误你能提到吗
  • 我假设您来自 angular 的请求具有 /api/ 路径,对吗? /api/sample

标签: php laravel api post get


【解决方案1】:

好的,这就是答案,毕竟这个错误根本不是错误。如果这个所谓的“错误”出现在我的屏幕上,它只意味着我的 API 或路由正在工作我试图将它调用到我的角度项目中并且它成功了!

有时这不是错误 谢谢大家的宝贵时间

【讨论】:

  • 等等,你是说你试图直接在浏览器中访问 API 端点并且不是当你得到那个错误时通过角度?如果是这种情况,您应该在问题中说明这一点,您会在几分钟内得到答复。然后这里的每个人都可以继续前进并帮助其他人,而不是回答错误的问题。下次请阅读How to create a Minimal, Reproducible Examplehow to ask并包括全部从一开始的信息。
【解决方案2】:

根据您的问题,此路由不支持 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。

【讨论】:

  • name() 的用途是什么?
  • 当 OP 声明他们正在使用 Angular 时,你为什么要在 jQuery 中举一个例子?
  • @M.Eriksson 正是
  • @M.Eriksson 我只是给他提示如何解决问题。作为一名开发人员,他应该了解如何在 Angular 或 Vue js 或其他任何应用中应用此逻辑。
【解决方案3】:

我遇到了同样的问题,但我的项目只使用 laravel。我使用“post”作为在我的数据库中发送数据的方法,但总是出现相同的错误。你能帮帮我吗?enter image description here

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 2019-08-28
  • 2021-04-11
  • 2019-08-31
  • 2021-05-05
  • 1970-01-01
相关资源
最近更新 更多