【问题标题】:POST method not supported using Laravel 6 and ReactJS?使用 Laravel 6 和 ReactJS 不支持 POST 方法?
【发布时间】:2020-02-26 21:18:25
【问题描述】:

我正在尝试使用 Laravel 6 和 ReactJS 将一些数据添加到我的 MySQL 数据库中。

我收到此错误:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: GET, HEAD.

我不确定问题出在哪里,因为我在路由中有 POST 方法。

这是我的文件:

路由:web.php

Route::get('/{any?}', function () {
    return view('welcome');
});

Route::post('/addproduct', 'StoreProductsController@store');

型号:Products.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Products extends Model
{
    protected $fillable = ['name', 'stock', 'broken'];
}

控制器:StoreProductsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Products;

class StoreProductsController extends Controller



{
    public function store(Request $request)
    {
        $products = new Products();

        $products->name = request('name');
        $products->stock = request('stock');
        $products->broken = request('broken');
        $products->save();

        return redirect('/storage');
    }
}

ReactJS 组件:Storage.js

<form method="post" action="/addproducts">

    <div class="form-row">
         <div class="form-group col-sm-12 col-md-6">
              <label for="inputNombreProductos">Producto</label>
                  <input type="text" class="form-control" id="inputNombreProductos" placeholder="Nombre del producto..." name="name"></input>
          </div>
          <div class="form-group col-sm-12 col-md-3">
               <label for="inputCantidadProductos">Cantidad</label>
                    <input type="text" class="form-control" id="inputCantidadProductos" placeholder="Cantidad de productos..." name="stock"></input>
           </div>

            <div class="form-group col-sm-12 col-md-3">
                  <label for="inputNombreProductos">Dañados</label>
                        <input type="text" class="form-control" id="inputNombreProductos" placeholder="Nombre del producto..." name="broken"></input>
            </div>
     </div>
     <button type="submit" className="btn btn-primary">Agregar</button>

如果有人知道如何解决这个问题,我将不胜感激。提前致谢。

当我运行命令 php artisan route:list 这是我得到的:

| Domain | Method   | URI        | Name | Action                                             | Middleware   |
+--------+----------+------------+------+----------------------------------------------------+--------------+
|        | POST     | addproduct |      | App\Http\Controllers\StoreProductsController@store | web          |
|        | GET|HEAD | api/user   |      | Closure                                            | api,auth:api |
|        | GET|HEAD | {any?}     |      | Closure                                            | web          |

【问题讨论】:

  • php artisan route:list 的输出是什么
  • 添加到主帖。
  • 这是一个错字:/addproducts vs /addproduct。将删除我的答案
  • 你有它。
  • 已经纠正错字,现在我得到 419 Page Expired

标签: reactjs laravel


【解决方案1】:

你不应该创建这样的通配符路由

Route::get('/{any?}', function () {
    return view('welcome');
});

因为您所有的无效路线都会映射到这条路线。但是如果由于某种原因你需要把它移到你的路由文件的末尾。

【讨论】:

  • 我正在使用该通配符路由,因为我正在使用 React Router 制作 SPA。
  • 刚刚注意到您的组件表单操作中有一个类型
【解决方案2】:

您可以在控制器中使用注释来添加 POST 方法吗? 在 symfony 中我做到了:

/**
     * @Route("/add_notification/{email}", name="add_notification", methods={"POST"})
     * @param Request $request
     * @param SerializerInterface $serializer
     * @return Response
     * @throws Exception
     */

我不知道 Laravel 是否支持

【讨论】:

    猜你喜欢
    • 2020-04-25
    • 2020-01-21
    • 2020-01-23
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2021-04-11
    • 2020-12-05
    相关资源
    最近更新 更多