使用Laravel 6和ReactJS不支持POST方法?
我正在尝试使用Laravel6和ReactJS向我的MySQL数据库添加一些数据 我得到这个错误:使用Laravel 6和ReactJS不支持POST方法?,reactjs,laravel,Reactjs,Laravel,我正在尝试使用Laravel6和ReactJS向我的MySQL数据库添加一些数据 我得到这个错误: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The POST method is not supported for this route. Supported methods: GET, HEAD. 我不确定问题出在哪里,因为我在路由中有POST方法 这是我的档案: 路由:web.php Route:
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');
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
protected $fillable = ['name', 'stock', 'broken'];
}
<?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');
}
}
模型:Products.php
Route::get('/{any?}', function () {
return view('welcome');
});
Route::post('/addproduct', 'StoreProductsController@store');
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
protected $fillable = ['name', 'stock', 'broken'];
}
<?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');
}
}
您不应该创建这样的通配符路线
Route::get('/{any?}', function () {
return view('welcome');
});
因为所有无效路由都将映射到此路由。但是,如果出于某种原因,您需要使用它,请尝试将其移动到路由文件的末尾。您可以使用控制器中的注释来添加POST方法吗? 在symfony,我做到了:
/**
* @Route("/add_notification/{email}", name="add_notification", methods={"POST"})
* @param Request $request
* @param SerializerInterface $serializer
* @return Response
* @throws Exception
*/
我不知道Laravel是否支持这一点php artisan route:list的输出添加到主要帖子中。这是一个打字错误:
/addproducts
vs/addproduct
。将删除我的答案。你有它。已经更正了typpo,现在我得到419页过期我使用通配符路由,因为我使用React路由器来创建SPA。刚刚注意到在你的组件表单操作中有一个类型