Laravel Ajax内部Servo 500(内部服务器错误) [英] Laravel ajax internal servor 500 (internal server-error)
问题描述
我有一个存储在会话中的购物车,我想刷新会话而不重新加载页面 我已经试过了: 查看:
I have a shopping cart which is stored in session and I want to refresh the session without reloading the page I have tried this: View:
<a href="#" id="product" data-id="{{ $product->id }}" class="item_add single-item hvr-outline-out button2">Add to cart</a>
<script>
$(document).ready(function() {
$('#product').click(function(event) {
event.preventDefault();
let url = "{{ route('add-to-cart') }}";
let id = $(this).data('id');
$.ajax({
url: url,
type: 'POST',
data: {product_id: id, _token: "{{ Session::token() }}"}
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
});
});
路线:
Route::post('/add-to-cart', 'ProductsController@addToCart')->name('add-to-cart');
ProductsController:
ProductsController:
public function addToCart(Request $request)
{
if ($request::ajax()) {
$id = $request->product_id;
$product = Product::find($id);
if (Session::has('products')) {
$products = Session::get('products');
$products[] = $product;
Session::put('products', $products);
}
else {
$products = array($product);
Session::put('products', $products);
}
return response()->json();
}
}
当我单击添加到购物车"时,它在控制台中显示500(内部服务器错误)
And when I click add to cart it gives 500 (Internal Server Error) in the console
推荐答案
当您应该改为使用->
时,您正在静态访问ajax()
方法(使用::
):
You're accessing the ajax()
method statically (using ::
), when you should be using ->
instead:
if ($request->ajax()) {
使用Laravel日志文件
如评论中所述,Laravel可能在storage/logs/laravel.log
中告诉了您这一点,并附带了很长的调用堆栈跟踪(您提到的行,以#38"和#39"开头).只需向上滚动到#1"之前,您就会找到罪魁祸首.
As mentioned in the comments, Laravel is probably telling you this in storage/logs/laravel.log
, complete with a long call-stack trace (the lines that you mentioned, beginning with "#38" and "#39"). Just scroll up to before "#1" and you'll find your culprit.
这篇关于Laravel Ajax内部Servo 500(内部服务器错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!