AJAX 在 Laravel 5.4 中不起作用
Doesn't work AJAX in Laravel 5.4
你能帮帮我吗?我的代码不起作用。 JS 警报 'succes' 但页面上没有任何变化。谢谢
Route
Route::post('/more', 'IndexController@index');
jQuery
$('#moreBtn').click(function () {
$.ajax({
method: 'post',
url: '/more',
data: 'fas',
async: true,
success: function(){
alert('succes');
},
error: function(data){
console.log(data);
alert("fail" + ' ' + this.data)
},
});
});
Controller
class IndexController extends Controller
{
public function index(Request $request)
{
$count = 8;
$videos = Video::leftJoin('users', 'videos.user_id', '=', 'users.id')
->select('users.id', 'users.name', 'videos.*')
->orderBy('created_at', 'desc')
->take($count)
->get();
if ($request->ajax()) {
$count += 4;
}
return view('welcome', array(
'videos' => $videos
));
}
}
就像我说的,它是 return 'succes',像 ajax 没问题,但不会改变我的页面。
数据没有改变,因为你没有改变它。
你的 ajax 应该更像
$.ajax({
method: 'post',
url: '/more',
data: 'fas',
async: true,
success: function(response){
alert('succes');
$('body').html(response) // or to the id/class you would like to change
},
error: function(data){
console.log(data);
alert("fail" + ' ' + this.data)
},
});
还有一件事,当您执行 ajax post 请求时,最好保留并使用 dat
传递令牌
你能帮帮我吗?我的代码不起作用。 JS 警报 'succes' 但页面上没有任何变化。谢谢
Route
Route::post('/more', 'IndexController@index');
jQuery
$('#moreBtn').click(function () {
$.ajax({
method: 'post',
url: '/more',
data: 'fas',
async: true,
success: function(){
alert('succes');
},
error: function(data){
console.log(data);
alert("fail" + ' ' + this.data)
},
});
});
Controller
class IndexController extends Controller
{
public function index(Request $request)
{
$count = 8;
$videos = Video::leftJoin('users', 'videos.user_id', '=', 'users.id')
->select('users.id', 'users.name', 'videos.*')
->orderBy('created_at', 'desc')
->take($count)
->get();
if ($request->ajax()) {
$count += 4;
}
return view('welcome', array(
'videos' => $videos
));
}
}
就像我说的,它是 return 'succes',像 ajax 没问题,但不会改变我的页面。
数据没有改变,因为你没有改变它。 你的 ajax 应该更像
$.ajax({
method: 'post',
url: '/more',
data: 'fas',
async: true,
success: function(response){
alert('succes');
$('body').html(response) // or to the id/class you would like to change
},
error: function(data){
console.log(data);
alert("fail" + ' ' + this.data)
},
});
还有一件事,当您执行 ajax post 请求时,最好保留并使用 dat
传递令牌