我正在尝试点击按钮获取数据。
这是我在控制器中返回数据的方式:
public function fetch()
{
$channels = Channel::where('channel', \Auth::user()->username)
->orderBy('created_at','desc')->take(3)->get();
View::share ('channels', $channels);
}
这是jQuery文件:
$( document ).ready(function() {
$('#dropdownMenu').click(function() {
$.ajax({
type: "GET",
dataType: "html",
url: "channels/fetch",
beforeSend: function() {
$('.testdropdown').html('loading please wait...');
},
success: function(htmldata) {
console.log(htmldata);
}
});
});
});
'loading please wait'部分正在运行,但是在成功的时候,我无法提示它。我缺少什么?
答案 0 :(得分:2)
你的Laravel方法:
public function fetch()
{
$username = \Auth::user()->username;
$channels = Channel::where('channel', $username)->orderBy('created_at', 'desc')->take(3)->get();
// Return as json
return Response::json($channels);
}
和你的javascript。
$(document).ready(function() {
$('#dropdownMenu').on('click', function() {
// Add loading state
$('.testdropdown').html('Loading please wait ...');
// Set request
var request = $.get('/channels/fetch');
// When it's done
request.done(function(response) {
console.log(response);
});
});
});