首页 > 解决方案 > Laravel AJAX - 如何在 console.log 中显示错误

问题描述

我想显示错误,或者想知道为什么这段代码在错误中运行:function()

结果总是运行到错误:函数。我想运行 success:function(data) 并重新加载此页面。

但是控制台没有显示任何关于错误的信息。

https://imgur.com/ZubjYTc

https://imgur.com/mSfHnSR

====== 阿贾克斯 ======

function ex_go(r_idx)
{
    if(confirm("Are you sure?") == true)
    {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $.ajax({
            type:'POST',
            dataType: 'JSON',
            url: "{{ route('change-centerYn') }}",
            data:{r_idx:r_idx},
            success:function(data){
                alert(data.success);
                location.reload();
            },
            error:function(xhr, data){
                console.log(xhr);
            },

    }else{
        return false;
    }
}

====== 控制器 ======

public function ex_ok(Request $request)
    {
        if(request()->ajax())
        {
            $r_idx = 'Hello';
            var_dump('<pre>', $r_idx);

            return response()->json(['msg'=>'Updated Successfully', 'success'=>true]);
        }
}

标签: ajaxlaravel-5.2

解决方案


它突然起作用了!难以置信的!非常感谢!你救了我的早晨!

使用尝试捕获

========= AJAX ===========

function ex_go(r_idx)
{
    if(confirm("해당 결제건을 지원센터로 보내시겠습니까?") == true)
    {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        console.log(r_idx);

        $.ajax({
            type:'POST',
            dataType: 'JSON',
            url: "{{ route('change-centerYn') }}",
            data:{r_idx:r_idx},
            success:function(data){
                alert(data.success);
                location.reload();
            },
            error:function(xhr, data){
                console.log(xhr);
            }
        });
    }else{
        return false;
    }
}

========= Laravel 控制器 =======

    public function ex_ok(Request $request)
    {
        try
        {
            if(request()->ajax())
            {
                $r_idx = $request->r_idx;

                $lecture = DB::table('class_order')
                ->select('*')
                ->where('r_idx', '=', $r_idx)
                ->first();

                if ($lecture->r_oid != '') {
                    $insert_data = [
                        'r_oid' => $lecture->r_oid,
                        'r_user_id' => $lecture->r_user_id,
                        'r_name' => $lecture->r_name,
                        'r_tel' => $lecture->r_tel,
                        'r_hp' => $lecture->r_hp,
                        'r_email' => $lecture->r_email,
                        'r_zip' => $lecture->r_zip,
                        'r_addr1' => $lecture->r_addr1,
                        'r_addr2' => $lecture->r_addr2,
                        'r_class' => $lecture->r_class,
                        'r_enddate' => $lecture->r_enddate,
                        'app_endday' => $lecture->app_endday,
                        'whole_study' => $lecture->whole_study,
                    ];
                    DB::table('ex_class_order')->insert($insert_data);
                    ClassOrder::where('r_idx', '=', $r_idx)->update(['centerYn' => 'y']);

                    $info_txt = "처리되었습니다.";
                }
                else
                {
                    $info_txt = "처리실패";
                }
                return response()->json(['msg'=>'Updated Successfully', 'success'=>true]);
            }
        }
        catch(\Exception $e)
        {
            \Log::error($e); // create a log for error occurrence at storage/log/laravel.log file
            return response()->json($e->getData(), $e->getStatusCode());
        }
}

推荐阅读