无法将 ajax 数据发送到 Laravel 控制器

Unable to send ajax data to Laravel controller

我只是想将 editable table 行数据发送到 Save 按钮的控制器 onClick,更新数据库中的数据, return 成功。

但是我无法在laravel的控制器函数中显示数据。 saveMe 函数内的数据如以下屏幕截图所示按需发送,但不会发送到控制器

<table id="customersTable" class="table table-bordered table-responsive-md table-striped text-center" style="border-style: solid; border-color:red">
      @php
         $customersData = Session::get('data');
         $issues = Session::get('issues');
      @endphp
      <thead>
         <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Contact</th>
            <th>Address</th>
            <th>Name</th>
         </tr>
       </thead>
       <tbody id="bodyData">
       @foreach ($customersData as $key => $data)
          <form action="ajaxform">
              <!-- This is our clonable table line -->
              <tr>
                 <td>{{$key}}</td>
                 <td name="name" class="pt-3-half name" contenteditable="true"                                                                                 
                                        value={{$data['name']}} data-id={{$key}}> 
                                        {{$data['name']}}
                 </td>
                 <td name="email" class="pt-3-half email" contenteditable="true"
                                        value={{$data['name']}} data-id={{$key}}>
                                        {{$data['email']}}
                 </td>
                 <td>
                     <div class="test">
                       <span class="table-save">
                         <button type="button" onclick="saveMe(this)" class=" btn btn-secondary btn-rounded btn-sm my-0 saveBtn">
                            Save
                         </button>
                        </span>
                     </div>
                  </td>
                </tr>
             </form>
         @endforeach
       </tbody>
    </table>

JavaScript 函数

<script>
    function saveMe(params) {
    var tr = $(this).closest("tr"); //get the parent tr
    var name = $(params).closest("tr").find(".name").text();
    var email = $(params).closest("tr").find(".email").text();
    console.log(name);
    console.log(email);
  
    $.ajax({
        url: '/customers/saveSingleRecord',
        type: 'GET',
        data: {
            _token:'{{ csrf_token() }}', 
            value: {
                'name' : name,
                'email' : email,
                'contact' :  contact,
                'address' : address,
            },
        },
        success: function(data){
            alert("success");
        }    
    });
}

函数里面controller

class CustomersController extends Controller
{
    public function saveSingleRecord(Request $request)
    {

        // $name = $_GET['name'];
        $name = $request->name;
        dd($name); // <------------------ Not showing anything
        // return response()->json($name);
    }
}

内部路线web.php

Route::post('/customers/saveSingleRecord/', [CustomersController::class, 'saveSingleRecord']);

正确的发送方式ajax如下

$.ajax({
        url: '/customers/saveSingleRecord',
        type: 'GET',
        data: {
                name : name,
                email : email,
                contact :  contact,
                address : address,
                _token :'{{ csrf_token() }}', 
        },
        success: function(data){
            alert("success");
        }    
    });

基本上您在 data.

中设置键值对

在您的 ajax 请求中,您将数据传递到 value 属性中,因此它没有显示。如果你尝试 $request->value['name'] 然后它会显示你的名字。如果您想直接在请求对象中获取名称,请按如下方式传递。

$.ajax({
        url: '/customers/saveSingleRecord',
        type: 'GET',
        data: {
            _token:'{{ csrf_token() }}', 
            'name' : name,
            'email' : email,
            'contact' :  contact,
            'address' : address,
        },
        success: function(data){
            alert("success");
        }    
    });