Here's how I do it with jQuery... Will use a very simple example. Let's say you have 2 elements, a #nav button, and a #container
First you prepare a route which is dedicated to loading ajax content..
Route::get('/ajax/GetContent', array(
'uses' => 'AjaxController@loadContent'
));
The controller in question basically loads content from the database (or whatever source you have), and returns html..
Now to the jquery part:
$(document).on('click','#nav', function(){
$.ajax({
url: rootPath + '/ajax/GetContent',
type: "GET", // not POST, laravel won't allow it
success: function(data){
$data = $(data); // the HTML content your controller has produced
$('#container').fadeOut().html($data).fadeIn();
}
});
});
I hope that was useful.. let me know if you need more..
Have a look at Pjax - it takes components of a normal view and updates the url. There's a Laravel Wrapper as well.
i think you can use post but in the route file you should use it like this
Route::post('/ajax/GetContent', array(
'uses' => 'AjaxController@loadContent'
));
not
Route::get('/ajax/GetContent', array(
'uses' => 'AjaxController@loadContent'
));
ferasallaou said:
i think you can use post but in the route file you should use it like this
Route::post('/ajax/GetContent', array( 'uses' => 'AjaxController@loadContent' ));
not
Route::get('/ajax/GetContent', array( 'uses' => 'AjaxController@loadContent' ));
lol exactly what i was thinking. Thanks btw, the ajax example will help me out to allow submitting POST data without refreshing the page for my users
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community