2017-06-17 59 views
1

我想发布一个GET请求来laravel路线,但我得到一个404(未找到)爱可信:发布带有参数的GET请求在Laravel

Ajax调用

axios.get('/statisticsJSON', { 
     params: { 
      annee: 2017, 
      mois: 06, 
      jour: 15, 
     } 
     }) 
     .then(function (response) { 
     console.log('Les donnees via ajax'); 
     console.log(response.data); 
     }) 
     .catch(function (error) { 
     console.log(error); 
     }); 

web.php

Route::get('/statisticsJSON/{annee}/{mois}/{jour}', '[email protected]') 
    ->name('statsJSON') 
    ->where('annee', '^(19|20)\d{2}$') 
    ->where('mois', '^(19|20)\d{2}$') 
    ->where('jour', '^(19|20)\d{2}$'); 

控制器

public function showStatisticsJSON(Request $request, $annee=null, $mois=null, $jour=null) 
{ 
    // 
    $annee = $request->get('annee'); 
    $mois = $request->get('mois'); 
    $jour = $request->get('jour'); 
    $emplois = Emploi::whereYear('POSTDATE', '=', $annee) 
      ->whereMonth('POSTDATE', '=', $mois) 
      ->whereDay('POSTDATE', '=', $jour) 
      ->get(); 
    return response()->json(emplois ,200,[],JSON_PRETTY_PRINT); 
} 

链接生成

http://localhost:8000/statisticsJSON?annee=2017&mois=6&jour=15 
+0

请不要说“发布请求”,嘿。 POST和GET是两种完全不同类型的HTTP请求 - 非常混淆的术语。 – ceejayoz

回答

1

您正在发送查询参数,而不是原样路由参数。 Laravel预计

http://localhost:8000/statisticsJSON/2017/6/15

,以便它匹配。首先为axios.get(my_url,构建网址字符串,并省略params: {}

+0

谢谢btl!但我kindda简化了我的代码,所以我会需要我的'params'因为每次我点击一个日期我需要检索'jour = date.getDate(); mois = date.getMonth()+ 1; annee = date.getFullYear();'所以网址将是动态的 –

+0

好的,谢谢我得到了查询参数和路由参数之间的差异 –

+1

好听,很高兴我可以帮助= D – btl