我在laravel中有一个用户列表。所以我想当我单击表格行时,它自动从数据库加载用户详细信息,而无需重定向。我听说过Ajax。但我不知道如何在Laravel中使用它。
我想这样
但没有重定向页面。
$("tr").click(function()
{
$.ajax({
type: "GET",
url: $(this).data("href"),
dataType: "text",
success : function(data) {
$('#details-container').load(data)
}
});
});
它应该是这样吗?但是我如何从数据库中获取数据?我不知道。请帮我..
它在Laravel中与在“本地” PHP中相同。使用Ajax请求,您可以发送和异步请求,并刷新网站的一小部分,因此您无需重新加载整个页面。
如果要使其与laravel一起使用,则应:
为您的请求创建路线
Route::get('user/{id}', function($id)
{
// So all your GET requests with any user id goes to this script (in routes.php)
// here you should load your user information, maybe with ORM
$user = User::where('id','=',$id)->get();
// Build any HTML snippet, or build JSON data (and build the information in Javascript on your page)
//return here JSON / or html
$data = array() -> containts information for the blade template
return view('users.information', $data);
});
您的AJAX请求现在返回JSON /或HTML,并将其附加到您想要显示它的站点上。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句