简体   繁体   English

Laravel Ajax Post请求返回空数组

[英]Laravel Ajax Post request returns empty array

So for this assignment, I need to Add a progress bar for a Dropify uploader in a Laravel application. 因此,对于此分配,我需要在Laravel应用程序中为Dropify上传器添加进度条。 But it cannot seem to find the session according to the key, when I print out the session with the key, all it does is return an empty Array ( ). 但是它似乎无法根据键找到会话,当我用键打印会话时,它所做的只是返回一个空的Array()。

My Form 我的表格

{{ Form::open(array('url' => route('admin.templates.store'), 'files' => true, 'id' => 'myForm')) }}
            <input type="hidden" value="myForm" name="{{ ini_get('session.upload_progress.name') }}">

                <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                    {{ Form::text('name', null, ['class' => 'form-control', 'required' => 'required']) }}
                    {{ Form::label('name', 'Template name', ['class' => 'control-label']) }}
                    <i class="bar"></i>
                    {{ $errors->first('name', '<p class="help-block">:message</p>') }}
                </div>

                <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                    {{ Form::textarea('description', null, ['class' => 'form-control', 'required' => 'required', 'size' => '1x1']) }}
                    {{ Form::label('description', 'Template omschrijving', ['class' => 'control-label']) }}
                    <i class="bar"></i>
                    {{ $errors->first('description', '<p class="help-block">:message</p>') }}
                </div>

                <div class="form-group{{ $errors->has('number_of_screens') ? ' has-error' : '' }}">
                    {{ Form::number('number_of_screens', '1', ['class' => 'form-control', 'required' => 'required']) }}
                    {{ Form::label('number_of_screens', 'Aantal objecten', ['class' => 'control-label']) }}
                    <i class="bar"></i>
                    {{ $errors->first('number_of_screens', '<p class="help-block">:message</p>') }}
                </div>

                <div class="form-group{{ $errors->has('templatefile') ? ' has-error' : '' }}">
                    {{ Form::label('templateFile', 'Browse') }}
                    {{ Form::file('templateFile' , ['class' => 'dropify', 'data-allowed-file-extensions' => 'zip', 'required' => 'required']) }}
                    <div id="bar_blank">
                       <div id="bar_color"></div>
                    </div>
                    <div id="status"></div>
                </div>

                <div class="button-container">
                    {{ Form::submit('Uploaden', ['class' => 'button button-medium']) }}
                </div>

            {{ Form::close() }}

My Ajax request 我的Ajax请求

$.ajax({
                        type: "POST",
                        url: "{{route('admin.templates.uploadprogress')}}",
                        data: {
                            "data": $('#myForm').serialize(),
                            "_token": "{{ csrf_token() }}",
                        },
                        success: function() {
                            console.log("Geodata sent");
                        }
                    });

My route 我的路线

Route::post('/uploadprogress', 'AdminTemplateController@callSessionData')->name('uploadprogress');

My controller function 我的控制器功能

public function callSessionData(Request $request){
    $key = ini_get("session.upload_progress.prefix") . "myForm";
    print_r($_SESSION);

    if (!empty($_SESSION[$key])) {
        $current = $_SESSION[$key]["bytes_processed"];
        $total = $_SESSION[$key]["content_length"];
        echo $current < $total ? ceil($current / $total * 100) : 100;
    }
    else {
        echo 100;
    }
}

I've asked more people before and they don't seem to know the issue either, so if someone could help me with this issue I would me more than happy. 我之前问过更多的人,他们似乎也不知道这个问题,因此,如果有人可以帮助我解决这个问题,我会很高兴。

a couple of things, in your ajax you shouldn't use php why don't you select the route already created in your form, it would be way more clean. 有两件事,在您的ajax中,您不应该使用php,为什么不选择已在表单中创建的路由,这样会更干净。

Maybe it returns nothing because this is not how you should access SESSION on laravel. 也许它什么也不返回,因为这不是您应该在laravel上访问SESSION的方式。

You should do it like this: 您应该这样做:

$yourSessionArray = $request->session()->get('key');

Please read about how to use SESSION with laravel on their docs. 在他们的文档中了解如何将SESSION与laravel一起使用

In Url use url: "{{route('/uploadprogress')}}", and in Route use Route::get() not Route::post() like: 在网址中使用url: "{{route('/uploadprogress')}}",在Route中使用Route::get()而不是Route::post()像:

Route::get('/uploadprogress', 'AdminTemplateController@callSessionData')->name('uploadprogress');

and in Ajax use: 在Ajax中使用:

$.ajax({
    type: "POST",
    url: "/uploadprogress",
    data: {
        "data": $('#myForm').serialize(),
        "_token": "{{ csrf_token() }}",
    },
    success: function() 
    {
        console.log("Geodata sent");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM