2017-10-10 94 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函數。 –