Ajax使用laravel和路由调用get数据

时间:2017-10-06 14:29:51

标签: php jquery ajax laravel routes

我有使用核心PHP进行ajax调用的经验。现在我想要laravel框架。所以很难实现。实际上我有

路线的来源
"source: "{{route('client.details')}}","

我想把这条路线放在网址上,但是我怎么不知道。我尝试过如下,

$.ajax({
method: 'GET', 
url: '/client/details'
success: function(response){ // What to do if we succeed
$('#invoiceNo').val(response.item.invoiceNo);
$('#DueDate').val(response.item.DueDate); 
}
});

控制器代码

public function search2(Request $request)
{ 
$s= Input::get('term');
$clients = Invoice::select("invoiceNo" ,"Total", "invoiceDate", "DueDate" )->where('status',['sent,Partially paid'])->where('client_id',$request->id)->get();
if(count($clients) == 0){
$searchResult[] = "No Item found";
}
else{
foreach ($clients as $key => $value) {//fill here too
$searchResult[] = ['invoiceNo' => $value->invoiceNo, 'Total' => $value->Total , 'invoiceDate' => $value->invoiceDate , 'DueDate' => $value->DueDate];
}
} 
return $searchResult; 
}

我的路线是

Route::get('/client/search/details', 'ClientsController@search2')->name('client.details');

我不知道是什么问题。我在ajax url部分得到错误。任何人都可以建议我解决方案是什么? 提前致谢

2 个答案:

答案 0 :(得分:0)

AJAX适用于客户端,因此它不知道您的命名路由。命名路由在Laravel内部的服务器端工作。

你必须告诉AJAX获取完整的URL:

$.ajax({ method: 'GET', url: '/client/search/details' ...

答案 1 :(得分:0)

url:' / client / details' 替换为 url:" {{route(' client.details')} }",

然后完整的代码在ajax部分看起来像这样: -

method: 'GET', 
url: "{{route('client.details')}}",
success: function(response){ // What to do if we succeed
$('#invoiceNo').val(response.item.invoiceNo);
$('#DueDate').val(response.item.DueDate); 
}
});