不允许使用补丁方法!有替代的请求方法吗?

时间:2020-07-18 07:38:59

标签: laravel methods request axios patch

我不断收到此错误

错误

补丁http:// localhost / cart / 027c91341fd5cf4d2579b49c4b6a90da 405(不允许使用方法)

我读到某处未在服务器上启用补丁方法,我正在使用Apache 2.4.41,我试图找到一种通过mod_allowmethods模块启用该方法的方法,但是该文件无法编辑或修改。在这一点上,我陷入困境,将不胜感激。

这是尝试更新购物车数量的代码

HTML

<div>
        <select class="quantity" data-id= "{{ $item->rowId }}">
          <option value="">1</option>
          <option value="">2</option>
          <option value="">3</option>
          <option value="">4</option>
          <option value="">5</option>
        </select>
      </div>

JavaScript

    <script>
    (function(){
        const classname = document.querySelectorAll('.quantity')

        Array.from(classname).forEach(function(element) {

          element.addEventListener('change', function() {

            const id = element.getAttribute('data-id')

            axios.patch(`/cart/${id}`, {

              quantity: this.value
            })
            .then(function(response) {

              windows.location.href = '{{ route('cart.index') }}'
            })

            .catch(function(error) {
              console.log(error);
            })
          })

        })
    })();
</script>

路线

Route::patch('/cart/{product}', 'CartController@update')->name('cart.update');

购物车控制器

public function update(Request $request, $id)
{
    Cart::update($id, $request->quantity);

    session()->flash('success', 'Quantity was updated successfully');
    
    return response()->json(['success' => true]);
}

2 个答案:

答案 0 :(得分:0)

使用发布方法并将补丁作为数据发送。

axios.post('/cart/${id}', {
    quantity: this.value,
    _method: 'patch',
    _token: '{{ csrf_token() }}' 
})
.then(function(response) {
    windows.location.href = '{{ route('cart.index') }}'
})
.catch(function(error) {
    console.log(error);
})

答案 1 :(得分:0)

尝试以下代码: 添加csrf令牌

<meta name="csrf-token" content="{{ csrf_token() }}">

并删除“ /”:/cart/${id}

axios.patch(`cart/${id}`, {
                quantity : this.value
            })
                .then(function (response) {
                    console.log(response);
                })
                .catch(function (error) {
                    console.log(error);
                });