samedi 2 mai 2020

Ajax call on change not working in laravel 5.8

web.php file

Route::group(['middleware'=>['auth','lfwuser']], function(){
    Route::get('/lfwuser_addEngagementData', 'LfwUser\LfwDashboardController@getEngagementData');

    Route::post('/lfwuser_SubDataEngagementData', 'LfwUser\LfwDashboardController@fetchSubData');
});

LfwDashboardController File.

public function getEngagementData(){
        if (Auth::check()) {
            $data = DB::table('table')
                            ->select('col')
                            ->distinct('col')
                            ->get();
            return view('lfwuser.lfwuser_addEngagementData')->with('data', $data);
        }
        else {
            return \view('auth.login');
        }

    }

    public function fetchSubData(Request $request){
        if (Auth::check()) {
            echo "Hello World";
        }
        else {
            return \view('auth.login');
        }

    }

Blade file

@extends('layouts.lfwuser') @section('title') LeapForWord | Content Management @endsection @section('content')
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <center>
                    <h4 class="card-title">Add Data</h4>
                </center>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3">
                    </div>
                    <div class="col-md-6">
                        <form action="/lfwuser_addEngagementData" method="POST" enctype="multipart/form-data ">
                             


                            <div class="form-group">
                                <label for="dataType">Data Type</label>
                                <select class="form-control" name="dataType" id="dataType" required>
                                    <option value=" ">--- Select ---</option>
                                    @foreach($data as $datatype)
                                    <option value=""></option>
                            @endforeach
                            </select>
                            </div>

                            <div class="form-group">
                                <label for="subdataType">Sub Data Type</label>
                                <select class="form-control" name="subdataType" id="subdataType" required>
                                    <option value="">--- Select ---</option>
                                </select>
                            </div>

                            <div class="form-group">
                                <input type="hidden" class="form-control" name="subDataId" id="subDataId" required>
                            </div>

                            <div class="form-group">
                                <label for="filellink">Link:</label>
                                <input type="text" class="form-control" name="filellink" id="filellink" required>
                            </div>

                            <div class="form-group">
                                <div class="row">
                                    <button type="submit" class="btn btn-outline-success text-center">Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-3"></div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection @section('scripts')
<script>
    $(document).ready(function() {
        $("#dataType").change(function() {
            let datatype = $("#dataType").val();
            let _token = $('input[name="_token"]').val();

            console.log("Data Type: " + datatype);

            if (dataType != '') {
                $.ajax({
                    url: "/lfwuser_SubDataEngagementData",
                    method: "POST",
                    dataType: "json",
                    data: {
                        datatype: datatype,
                        _token: _token
                    },
                    success: function(response) {
                        console.log("hi" + response);
                    }
                });
            } else {

            }

        });
    });
</script>
@endsection

Nothing gets in response and no error on the console. How should I resolve this issue? The form is shown when the user logged in into the system The blade file store inside the view/user folder. I guess the mistake in the URL part. URL is not calling properly, sometimes it shows error 419 for file, It is taking much time please let me know where I am doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire