我想使用axios补丁方法更新购物车中的项目数量,所以我想先在浏览器控制台中测试它,但是我一直收到这个错误。
误差
状态代码405的
请求失败。
路
Route::patch('/cart/{product}', 'CartController@update')->name('cart.update');手推车控制器
public function update(Request $request, $id)
{
return $request->all();
} <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
@section('extra-js')
<script src=" {{ asset('js/app.js') }} "></script>
<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) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
})
})
})();
</script>
@endsection发布于 2020-07-11 10:28:12
我认为您的问题在于您使用修补程序调用的url:
axios.patch('/cart/${id}',我假设您的刀片文件中有一个$id,并且希望:
axios.patch('/cart/{{ $id }}',或
你想要js的身份证:
axios.patch('/cart/'+id,或
您的语法正常,但id为空或未定义。
https://stackoverflow.com/questions/62847883
复制相似问题