繁体   English   中英

在Laravel 5.4中无法使用AJAX

[英]Doesn't work AJAX in 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可以,但不会更改我的页面。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM