2018-09-30 18:46
浏览 69

在Laravel 5.3中使用AJAX更新表无法正常工作

I trying to use an AJAX PUT request to update a row in my database and I am trying to send the request to my controller. This is my AJAX call:


    var truckNo = $('#XA').val();
    var truckOwner = $('#truck-owner').val();
    var vehicle_number = $('#vehicle-number').val();
    var capacity = $('#capacity').val();
    var end_of_insurance = $('#end-of-insurance').val();
    var end_of_kteo = $('#end-of-KTEO').val();
    var truckCode = $('#truck-code').val();
    var leased = $('#leased').val();
    var truckModel = $('#truck-model').val(); 

        url: 'editTruck',
        type: 'put',           
        data: {
            truckNo: truckNo,
            truckOwner: truckOwner,
            vehicle_number: vehicle_number,
            capacity: capacity,
            end_of_insurance: end_of_insurance,
            end_of_kteo: end_of_kteo,
            truckCode: truckCode,
            leased: leased,
            truckModel: truckModel
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        contentType: 'application/json',
        dataType: 'JSON',
        success: function(){
        error: function(){
           console.log('something went wrong');

So far so good. If I console.log() my data is seems I can get them from the form. I am using Laravel Collective for the form:

 {!!Form::open(array('action' => ['Trucks@editTruck'], 'method' => 'put')) !!}  

and my route is the following:

Route::put('/editTruck', 'Trucks@editTruck',function(){ });

Now I am using Request $request in the parameters of the controller but somehow it looks like I cannot get the incoming values. For example the following var_dump will say NULL.

public function editTruck(Request $request)
      $data = $request->input('truckNo');

Same happens if I use

$data = $request->truckNo;

instead. So I am wondering how can I get the values that are been sent to my controller with my AJAX call? Why am I getting NULL values?

What I was planning to do is:

 public function editTruck(Request $request)
       $singleTruck = Truck::find($request->truckNo);          
       $singleTruck->truckNo = $request->input('truckNo'); 
       $singleTruck->truckOwner = $request->input('truckOwner');
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题