英文:
The GET method is not supported for route post_wishlist. Supported methods: POST
问题
我正在为我的Laravel应用程序添加一个“添加到愿望清单”的路由,但当我尝试使用它时,出现“不支持的方法”的错误。
我已经添加了一个锚点标签,并在href中定义了它的路由:
<a href="{{ url('/post_wishlist') }}" id="addtowishlist"><i class="fa fa-heart fa-2x addtowishliststyle" aria-hidden="true" style="color:' + (cardimg['inWishlist'] ? '#8B0000' : 'white') + '"></i></a>
我还在web.php文件中创建了一个路由:
Route::post('/post_wishlist', [MyCartController::class, 'post_wishlist'])->name('post_wishlist');
public function post_wishlist()
{
echo "add to wishlist called";
}
点击链接时,我收到以下错误:
不支持post_wishlist路由的GET方法。支持的方法:POST
锚点标签是否有问题?
英文:
I am adding a route in my Laravel application for an "add to wishlist" feature, but I am getting "method not supported" when I try to use it.
I have added an anchor tag and defined its route in the href:
<a href="{{url('/post_wishlist')}}" id="addtowishlist"><i class="fa fa-heart fa-2x addtowishliststyle" aria-hidden="true" style="color:' + (cardimg['inWishlist'] ? '#8B0000' : 'white') + '"></i></a>
I have also created a route in the web.php file:
Route::post('/post_wishlist', [MyCartController::class, 'post_wishlist'])->name('post_wishlist');
public function post_wishlist()
{
echo "add to wishlist called";
}
When clicking the link I get the following error:
> The GET method is not supported for route post_wishlist. Supported methods: POST
Is there an issue with the anchor tag?
答案1
得分: 1
点击一个<a href
标签总是会导致浏览器生成一个HTTP GET请求。但是使用Route::post
,您定义了路由只接受HTTP POST请求。
要解决这个问题,您可以选择:
- 重新定义路由以接受GET请求,或者
- 用一个表单和一个按钮替换锚标签,它可以生成一个POST请求。
并作为背景任务,确保您理解HTTP方法是什么。https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
英文:
Clicking a <a href
tag always causes the browser to generate a HTTP GET request. But with Route::post
you defined your route as only accepting HTTP POST requests.
To resolve it you can either:
- redefine the route to accept GET, or
- replace the anchor tag with a form and a button which can generate a POST request.
And as a background task, make sure you understand what HTTP methods are. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
答案2
得分: 1
Use this instead Route::get
and <a href="{{route('post_wishlist')}}">
If you want to store data use Post ex.
Route::post('/post_wishlist', [MyCartController::class, 'post_wishlist'])->name('post_wishlist');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论