Laravel AJAX-如何在console.log中显示错误

时间:2019-06-10 07:25:28

标签: ajax laravel-5.2

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

结果总是运行到error:function。我要运行成功:功能(数据)并重新加载此页面。

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

https://imgur.com/ZubjYTc

https://imgur.com/mSfHnSR

======= Ajax ======

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]);
        }
}

2 个答案:

答案 0 :(得分:0)

由于您未使用TryCatch或错误处理,您的控制器将返回200 HTTP标头状态代码,这意味着ajax始终会认为该过程是正确的,并且指出出现了错误,请尝试使用控制器功能中的错误处理,如果在代码处理过程中出现问题,则会引发异常。您可以阅读此link

您可以在下面看到对现有代码的示例代码修改:

public function ex_ok(Request $request)
{
    try
    {
        if(request()->ajax())
        {
            $r_idx = 'Hello';
            var_dump('<pre>', $r_idx);
            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());
    }
}

答案 1 :(得分:0)

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

使用try catch

========== 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());
        }
}