GET方法不支持路由post_wishlist。支持的方法为:POST

huangapple go评论69阅读模式
英文:

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:

&lt;a href=&quot;{{url(&#39;/post_wishlist&#39;)}}&quot; id=&quot;addtowishlist&quot;&gt;&lt;i class=&quot;fa fa-heart fa-2x addtowishliststyle&quot; aria-hidden=&quot;true&quot; style=&quot;color:&#39; + (cardimg[&#39;inWishlist&#39;] ? &#39;#8B0000&#39; : &#39;white&#39;) + &#39;&quot;&gt;&lt;/i&gt;&lt;/a&gt;

I have also created a route in the web.php file:

Route::post(&#39;/post_wishlist&#39;, [MyCartController::class, &#39;post_wishlist&#39;])-&gt;name(&#39;post_wishlist&#39;);

public function post_wishlist()
{
    echo &quot;add to wishlist called&quot;;
}

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

点击一个&lt;a href标签总是会导致浏览器生成一个HTTP GET请求。但是使用Route::post,您定义了路由只接受HTTP POST请求。

要解决这个问题,您可以选择:

  • 重新定义路由以接受GET请求,或者
  • 用一个表单和一个按钮替换锚标签,它可以生成一个POST请求。

并作为背景任务,确保您理解HTTP方法是什么。https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

英文:

Clicking a &lt;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');

@csrf

英文:

Use this instead Route::get and &lt;a href=&quot;{{route(&#39;post_wishlist&#39;)}}&quot;&gt;

If you want to store data use Post ex.

    Route::post(&#39;/post_wishlist&#39;, [MyCartController::class, &#39;post_wishlist&#39;])-&gt;name(&#39;post_wishlist&#39;);
    
    
    &lt;form action=&quot;{{route(&#39;post_wishlist&#39;)}}&quot; method=&quot;POST&quot;&gt;
     @csrf

huangapple
  • 本文由 发表于 2023年6月27日 20:28:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定