在Laravel 5.4中不起作用

时间:2017-03-15 00:03:14

标签: php jquery ajax laravel-5 laravel-5.4

你可以帮帮我吗?我的代码不起作用。 JS警告'成功'但页面上没有变化。谢谢

  

路线

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)
        },

    });
});
  

控制器

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
    ));
}

}

就像我说的那样,它回归'成功',就像ajax一样好,但不会改变我的页面。

1 个答案:

答案 0 :(得分:1)

数据没有变化,因为您没有更改它。 你的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的令牌