Vad
Vad

Reputation: 48

Doesn't work AJAX in Laravel 5.4

could You, please, help me? My code is dorsn't work. JS alert 'succes' but it's no changes on page. Thank You

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

}

Like i said, it's return 'succes', like ajax is ok, but doesn't change my page.

Upvotes: 1

Views: 4334

Answers (1)

user2544565
user2544565

Reputation:

Data is not changing , because you are not changing it. Your ajax should more like

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

});

And one more thing, when you are doing ajax post request it is a good practice too keep and pass the token with the dat

Upvotes: 1

Related Questions