douyoufan7881 2018-09-30 18:46
浏览 70
已采纳

在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:

$('#edit-trucks').on('click',function(){

    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(); 

    $.ajax({
        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(){
            console.log('success');
        },            
        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');
      var_dump($data);
}

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');
       ........
       $singleTruck->save();
}
  • 写回答

2条回答 默认 最新

  • doukuangxiu1621 2018-10-02 19:18
    关注

    OK I found it. Looks like the AJAX was malformed. So here is how it should be written:

      $('#edit-trucks').on('click',function(){
    
        var truckNo = $('#XA').val();
        var truckOwner = $('#truck-owner').val();
        var vehicle_number = $('#vehicle-number').val();
        var capacity = $('#vehicle_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 = $('#leasing').val();
        var truckModel = $('#truck-model').val();
    
    
        var outGoingData = {
                '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,
              };
    
        var data = JSON.stringify(outGoingData);
    
    
        $.ajax({
            url: 'editTruck',
            type: 'POST',
            data: data,            <!-- The error was here. It was data: {data}-->
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },           
            contentType: 'application/json',
            dataType: 'JSON',            
            success: function(response){
                alert("The data should be "+ response);
            },            
            error: function(){
                console.log('skata');
            }         
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 echarts图表制作
  • ¥15 halcon根据玻璃面板纹路取区域