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条)

报告相同问题?

悬赏问题

  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题