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

$('#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');
            }         
        });
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doupeng8494 2018-09-30 19:10

    You can find the answer here:

    https://laravel.io/forum/02-13-2014-i-can-not-get-inputs-from-a-putpatch-request

    You should change your form method and method inside your js code to "post", and add extra field "_method" = "PUT"

    probably it can help.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题