简体   繁体   English

php,Laravel 7 - 无法从 AJAX 请求接收数据

[英]php, Laravel 7 - Unable to receive data from AJAX request

I'm still a newbie and currently learning how to use Laravel 7.我还是个新手,目前正在学习如何使用 Laravel 7。

My problem is, I tried to pass my data from the controller by using an AJAX request on my child page.我的问题是,我试图通过在我的子页面上使用 AJAX 请求从控制器传递我的数据。 I noticed that when I tried to pass the data from my child page, the page won't receive it but somehow it's working on a master page (where I didn't use any Blade directives).我注意到当我尝试从我的子页面传递数据时,该页面不会收到它,但不知何故它正在母版页上工作(我没有使用任何 Blade 指令)。 I tried to dd() the data in the controller and it does show that there is data.我试图 dd() 控制器中的数据,它确实显示有数据。 But it won't pass it to the child page.但它不会将它传递给子页面。 All the JS files and custom script that I push does come out on the child page.我推送的所有 JS 文件和自定义脚本都出现在子页面上。

blade刀刃

code

index.blade.php (child page) index.blade.php(子页面)

@extends('layouts.app')

@section('content')

<div class="container">
<div class="container mt-5">
        <h2 class="mb-4">Laravel 7 Yajra Datatables Example</h2>
        <table class="table table-bordered yajra-datatable">
            <thead>
                <tr>
                    <th class="text-center">#</th>
                    <th class="text-center">Name</th>
                    <th class="text-center">Batch</th>
                    <th class="text-center">Graduation Year</th>
                    <th class="text-center">Mobile</th>
                    <th class="text-center">Action</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>

@endsection

@push('child-scripts')
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
    <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
    
    <script type="text/javascript">
        $(function () {
      
      var table = $('.yajra-datatable').DataTable({
          processing: true,
          serverSide: true,
          ajax: "{{ route('alumni-list') }}",
          columns: [
              {data: 'DT_RowIndex', name: 'DT_RowIndex'},
              {data: 'name', name: 'name'},
              {data: 'batch_year', name: 'batch_year'},
              {data: 'graduation_year', name: 'graduation_year'},
              {data: 'contact_no', name: 'contact_no'},
              {
                  data: 'action', 
                  name: 'action', 
                  orderable: true, 
                  searchable: true
              },
          ]
      });
      
    });
    </script>
    @endpush 

AlumniController.php校友控制器.php

class AlumniController extends Controller
{
    public function index(Request $request)
    {
        // dd(Profile::latest()->get());
        if ($request->ajax()) {
            $data = Profile::latest()->get();
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function($row){
                    $btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
                    return $btn;
                })
                ->rawColumns(['action'])
                ->make(true);
        }
      
        return view('admin.users.index');
    }
}

Alumni Route校友路线

Route::get('alumni', [
        'uses' => 'AlumniController@index',
        'as' => 'alumni-list'
    ]);

Thank you for your time, sir.谢谢你的时间,先生。

Solved it after MORE googling done.完成更多谷歌搜索后解决了它。

Make sure to add defer inside your DataTable CDN.确保在 DataTable CDN 中添加 defer。

Example:例子:

<script src = "http://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js" defer ></script>

Here's the link where I found the answer: https://datatables.net/forums/discussion/50869/typeerror-datatable-is-not-a-function-datatable-laravel这是我找到答案的链接: https : //datatables.net/forums/discussion/50869/typeerror-datatable-is-not-a-function-datatable-laravel

For more understanding on why defer is added: https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html有关为什么添加 defer 的更多理解: https : //www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html

use Yajra\\DataTables\\Contracts\\DataTable;使用 Yajra\\DataTables\\Contracts\\DataTable; use Yajra\\DataTables\\Facades\\DataTables;使用 Yajra\\DataTables\\Facades\\DataTables; please use this two line in your controller请在您的控制器中使用这两行

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

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