Laravel 5:Ajax发布500(内部服务器错误)错误

时间:2019-07-26 18:01:02

标签: php jquery ajax laravel laravel-5

我正在尝试通过ajax提交数据(添加到购物车),但在控制台POST http://localhost/glutax9gs/cartupdate 500中出现错误(内部服务器错误)

我的代码有什么问题?是JavaScript还是控制器?


这是我的路线:

Route::get('/cartupdate', 'FrontEndController@cartupdate')->name('update.cart');
Route::post('/cartupdate', 'FrontEndController@cartupdate')->name('cart.update');

控制器代码在这里:

    public function cartupdate(Request $request)
    {

        if ($request->isMethod('post')){

            if (empty(Session::get('uniqueid'))){

                $cart = new Cart;
                $cart->fill($request->all());
                Session::put('uniqueid', $request->uniqueid);
                $cart->save();

            }else{

                $cart = Cart::where('uniqueid',$request->uniqueid)
                    ->where('product',$request->product)->first();
                //$carts = Cart::where('uniqueid',$request->uniqueid)
                        //->where('product',$request->product)->count();
                if (count($cart) > 0 ){
                    $data =  $request->all();
                    $cart->update($data);
                }else{
                    $cart = new Cart;
                    $cart->fill($request->all());
                    $cart->save();
                }

            }
            return response()->json(['response' => 'Successfully Added to Cart.','product' => $request->product]);
        }

        $getcart = Cart::where('uniqueid',Session::get('uniqueid'))->get();

        return response()->json(['response' => $getcart]);
    }

jQuery代码在这里:

    $(".to-cart").click(function(){

        var formData = $(this).parents('form:first').serializeArray();
        $.ajax({
            type: "POST",
            url: '{{route("cart.update")}}',
            data:formData,
            success: function (data) {
                getCart();
                $.notify(data.response, "success");
            },
            error: function (data) {
                console.log('Error:', data);
            }
        });
    });

查看代码在这里:

        <form>
            <p>
                {{csrf_field()}}
                @if(Session::has('uniqueid'))
                    <input type="hidden" name="uniqueid" value="{{Session::get('uniqueid')}}">
                @else
                    <input type="hidden" name="uniqueid" value="{{str_random(7)}}">
                @endif
      <input type="hidden" name="title" value="{{$product->title}}">
      <input type="hidden" name="product" value="{{$product->id}}">
      <input type="hidden" id="cost" name="cost" value="{{$product->price}}">
      <input type="hidden" id="quantity" name="quantity" value="1">
           @if($product->stock != 0 || $product->stock === null )
    <button type="button" class="button style-10 to-cart">Add to cart</button>
              @else
           <button type="button" class="button style-10 to-cart" disabled>Out Of Stock</button>
           @endif
            {{--<button type="button" class="button style-10 hidden-sm to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</button>--}}
            </p>
        </form>

2 个答案:

答案 0 :(得分:1)

根据您的评论,这似乎是您在非数组元素上使用count(),这是禁止的。您应该修改支票:

发件人:

if (count($cart) > 0 ){

收件人:

if (is_array($cart) && count($cart) > 0 ){

提示:检查$cart是数组之前,请检查其长度。

答案 1 :(得分:0)

问题出在$ cart变量返回null 我刚从

if (count($cart) > 0 ){
                    $data =  $request->all();
                    $cart->update($data);
                }

对此:

if ($cart){
                    $data =  $request->all();
                    $cart->update($data);
                }

和问题解决 感谢所有人:)