【问题标题】:return blade after execute ajax执行ajax后返回刀片
【发布时间】:2021-10-09 12:51:26
【问题描述】:

在 ajax 响应确定后,我正在托盘加载 @include() 刀片到 laravel 的其他刀片中。我正在选项卡窗格中进行统计,我需要将数据发送到我的控制器,为此我正在做 ajax 我有这个 ajax:

$("#createStatistcs").on('click', function(){
        let fromDate = $("#fromDate").val();
        let toDate = $("#toDate").val();
        let token = $('meta[name=csrf-token]').attr('content');

        $.ajax({
            type: 'GET',
            url: "{{ route('admin.llamadas.estadisticas') }}",
            data: { 'fromDate': fromDate, 'toDate': toDate },
            success: function(response){
                
            },
            error: function(xhr){
                alert(xhr.responseText);
            }
        });
    })

我的控制器中有这个:

public function index(Request $request)
    {
        $estadosLlamadas = EstadoLlamada::orderBy('desc')->get();

        if(isset($request->fromDate) && isset($request->toDate)){
            $fromDate = Carbon::parse($request->get('fromDate'));
            $toDate = Carbon::parse($request->get('toDate'));

            $fromDate = $fromDate->format('Y-m-d');
            $toDate = $toDate->format('Y-m-d');

        }else{
            $fromDate = new Carbon('first day of this month');
            $toDate = new Carbon('last day of this month');

            $fromDate = $fromDate->format('Y-m-d');
            $toDate = $toDate->format('Y-m-d');
        }

        $teleoperadoras = auth()->user()->whereIs('teleoperadora')->activos()->select(['id', 'nombre'])->orderBy('nombre', 'desc')->get();
        
        $array = [
            'toDate'   => $toDate,
            'fromDate' => $fromDate,
            'nombresEstados' => $estadosLlamadas->pluck('desc')->toArray(),
            'coloresEstados' => $estadosLlamadas->pluck('hex')->toArray()
        ];

        $query = Llamada::query()
            ->whereDate('llamada.created_at', '<=', $toDate)
            ->whereDate('llamada.created_at', '>=', $fromDate)
            ->whereIn('llamada.id_teleoperadora', $teleoperadoras->pluck('id'))
            ->join('users', 'llamada.id_teleoperadora', '=', 'users.id')->latest('llamada.created_at')->get();

        foreach($teleoperadoras as $teleoperadora) {
            $array['teleoperadoras'][] = $teleoperadora->nombre;
            $array['id_teleoperadoras'][] = $teleoperadora->id;
            $array[$teleoperadora->id]['resultados'] = [];
            $array['llamadas'][] = $query->where('id_teleoperadora', $teleoperadora->id)->count();

            $array['llamadasTodo'][$teleoperadora->id] = $query->where('id_teleoperadora', $teleoperadora->id);

            foreach($estadosLlamadas as $estado) {
                $array[$teleoperadora->id]['resultados'][] = $query->where('id_teleoperadora', $teleoperadora->id)->where('id_estado', $estado->id)->count();
            }
        }

        $array['nllamadas'] = $query->count();
        $roleUser = auth()->user()->getRoles()->first();

        $view = view('admin.llamadas.filtrado', [
                    'datos' => $array, 'estados' => $estadosLlamadas,
                    'teleoperadoras' => $teleoperadoras, 'roleUser' => $roleUser,
                ])->render();
        echo response()->json(['html'=>$view]);
    }

这个函数返回我需要的所有数据。但我希望不要重新加载我的页面并在此视图中生成图形。

我正在处理这个,不返回错误,但什么也不返回。我在谷歌上阅读,很多人说使用-&gt;render(),但我无法显示结果

感谢您的帮助和自述文件

【问题讨论】:

  • 不确定是否完全理解您的问题,但 response.data 包含您从 ajax 调用中获得的 json 响应。 (在您的 .success 函数中)所以 response.data.html 似乎是您正在寻找的。提示:console.log(response) 调试您的 ajax 响应

标签: php ajax laravel laravel-blade


【解决方案1】:

如果您的模板中有一个 div,您希望在其中显示此刀片视图,您可以这样做:

$("#createStatistcs").on('click', function(){
        let fromDate = $("#fromDate").val();
        let toDate = $("#toDate").val();
        let token = $('meta[name=csrf-token]').attr('content');

        $.ajax({
            type: 'GET',
            url: "{{ route('admin.llamadas.estadisticas') }}",
            data: { 'fromDate': fromDate, 'toDate': toDate },
            success: function(response){
                $('your-element').html(response.html);
            },
            error: function(xhr){
                alert(xhr.responseText);
            }
        });
    })

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 2018-02-13
    • 1970-01-01
    • 2020-07-19
    • 2015-12-09
    • 1970-01-01
    • 2021-01-25
    • 2015-05-20
    • 1970-01-01
    相关资源
    最近更新 更多