从数据库中检索数据并将其发送到 laravel 中的 ajax
Retrieve data from database and send it to ajax in laravel
我有 laravel 项目,我想从数据库中检索数据并将其发送到 ajax 函数
这是 show ($ip) 函数的代码
public function show($id)
{
$result = DB::table('radcheck')get();
return ( $result);
//
}
这是 ajax 的代码,它试图从该函数检索数据,但它不能
$('.edit-user').on('click', function() {
var id=$(this).data('id');
$.ajax({
type: 'GET',
url: '/users/' + id,
data: {
'_token': $('input[name=_token]').val(),
},
success: function(hr_request) {
alert(hr_request['username']);
},
error: function(hr_request) {
alert(hr_request['username']);
}
});
});
没有可以检索的数据,我想我必须使用 json 数据将数据从控制器发送到 ajax,但我如何将其作为 json 数据发送以及如何发送我可以将这个 json 数据处理成 ajax 函数
很简单..你可以像下面那样做:
public function show($id)
{
$result = DB::table('radcheck')->get();
return response()->json(['data' => $result]); // here 'data' is key to access $result in ajax response , it is optional.
}
在 ajax 响应中,您可以使用 console.log(hr_request.data) 对其进行控制并检查您的数据结果。此外,要访问 属性,您需要执行 hr_request.data.属性
希望对您有所帮助,谢谢。
我有 laravel 项目,我想从数据库中检索数据并将其发送到 ajax 函数 这是 show ($ip) 函数的代码
public function show($id)
{
$result = DB::table('radcheck')get();
return ( $result);
//
}
这是 ajax 的代码,它试图从该函数检索数据,但它不能
$('.edit-user').on('click', function() {
var id=$(this).data('id');
$.ajax({
type: 'GET',
url: '/users/' + id,
data: {
'_token': $('input[name=_token]').val(),
},
success: function(hr_request) {
alert(hr_request['username']);
},
error: function(hr_request) {
alert(hr_request['username']);
}
});
});
没有可以检索的数据,我想我必须使用 json 数据将数据从控制器发送到 ajax,但我如何将其作为 json 数据发送以及如何发送我可以将这个 json 数据处理成 ajax 函数
很简单..你可以像下面那样做:
public function show($id)
{
$result = DB::table('radcheck')->get();
return response()->json(['data' => $result]); // here 'data' is key to access $result in ajax response , it is optional.
}
在 ajax 响应中,您可以使用 console.log(hr_request.data) 对其进行控制并检查您的数据结果。此外,要访问 属性,您需要执行 hr_request.data.属性
希望对您有所帮助,谢谢。