2017-10-10 108 views
0

我想从js文件到laravel路由做一个jquery的帖子,但似乎它不工作,我不知道为什么。jquery post调用路由laravel不工作

我的主要目标是:从一个复选框被选中的表中获取所有ID,并在SQL上更改它们的列值。

所以,这里是我的JS功能:

function concludeAll() { 
     var arrayIds = []; 

     $('.checkbox1').each(function() { 
      var $this = $(this); 
      var $id = $this.attr('id'); 

      if ($this.is(":checked")) { 
       arrayIds.push($id); 
      } 
     }); 

     var json = { 
      "ids": arrayIds 
     }; 

     $.post('http://localhost:8000/controle/pending/concludeAll', 
      { 
       '_token': $('meta[name=csrf-token]').attr('content'), 
       ids: arrayIds 
      }) 
      .error(

      ) 
      .success(

      );  
} 

这是我的路线:

Route::group(['prefix' => '/controle'], function() { 
Route::post('/pending/concludeAll/', function() { 

    $input = Input::only('ids'); 
    $input = $input['ids']; 

    foreach($input as $id) { 
     $student = new App\Aluno(); 
     $student = $student->where('id', '=', $id)->first(); 
     $student->pending = '0'; 
     $student->save(); 
    } 

}); }; 

所以,如果我检查表上的几行,撞上调用该函数按钮,我的控制台没有任何反应。上网络>头>形式数据I看到的令牌和的ID,如下所示:

_token:fNWunwF8yDLSycrkBE684wgQcyK9dP8wbR7VgLjC IDS []:23个 IDS []:20

上预览,我看到我是完全一样的页面。 作为回应,我看到了页面的HTML。

我也试过dd($ input);在路线上,但没有什么不同发生..

尝试php工匠路线:清除和没有什么不同发生。

如果我更改URL为http://localhost:8000/controle/pending/concludeAll2的名称,返回任何错误,这让我疯狂......

任何想法如何让这个帖子调用路线?谢谢!

+0

转换你的'arrayIds'成JSON'IDS:JSON.stringify(arrayIds)'然后看.. –

+0

如果你的控制台没有任何反应,你怎么看到这些头文件? – Devon

+0

@HimanshuUpadhyay它唯一的区别是:在网络>标题>表单数据我看到ids:[“23”,“20”,“29”],而不是ids []:23 ids []:20。我认为这是必要的,但还没有解决问题。不过谢谢。 –

回答

0

尝试将路线更改为

Route::post('/controle/pending/concludeAll', function() { 
    $input = Input::only('ids'); 
    $input = $input['ids']; 
    foreach($input as $id) { 
     $student = new App\Aluno(); 
     $student = $student->where('id', '=', $id)->first(); 
     $student->pending = '0'; 
     $student->save(); 
    } 

}); 

和JS功能:

function concludeAll() { 
     var arrayIds = []; 

     $('.checkbox1').each(function() { 
      var $this = $(this); 
      var $id = $this.attr('id'); 

      if ($this.is(":checked")) { 
       arrayIds.push($id); 
      } 
     }); 
     arrayIds=JSON.stringify(arrayIds); 

     var json = { 
      "ids": arrayIds 
     }; 

     $.post('http://localhost:8000/controle/pending/concludeAll', 
      { 
       '_token': $('meta[name=csrf-token]').attr('content'), 
       ids: arrayIds 
      }) 
      .error(

      ) 
      .success(

      );  
    } 
+0

I'对不起,我忘了发布上面的路线是在Route :: group('controle')内。 –

+0

@GabrielAugusto可以请你发表形式,可能会给你一些线索。张贴方法的问题也可能导致类似的问题 –

+0

对不起,这里没有任何形式。这是一个我选择项目的表格,当我点击一个普通按钮时,它会调用具有post方法的javascript函数。 –