Common Ajax Function in Laravel 5

Ajax makes your application more flexible, you don’t need to reload or refresh the whole body for small changes, you can made changes in any part without loading page.

How to Call ajax in Laravel 5

File Upload in Laravel 5 using Ajax With Form Data

Html head declare in meta tag.

<meta name="csrf-token" content="{{ csrf_token() }}" />

Common Ajax function declare.

var xhr;
function request_call(url,mydata)
{
    if(xhr && xhr.readyState != 4){
        xhr.abort();
    }

    xhr = $.ajax({
        url: url,
        type :'post',
        dataType: 'json',
        data : mydata,
    });
}

Call to route using Ajax

$('#gerResult').delegate('.ViewDiamondDetail','click', function(event){
   var loatno = this.id;
   var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    request_call("{{url('view-diamond-detail')}}",'_token='+CSRF_TOKEN+'&loatno='+loatno);
    xhr.success(function( mydata ) {
         $('#myModal').html(mydata.success);
         $('#myModal').modal('show'); 
    });
});

Routes to Controller call

Route::post('view-diamond-detail', [
    'as' => 'view-diamond-detail',
    'uses' => 'SearchDiamondController@ViewDiamondDetail'
]);

Controller function

public function ViewDiamondDetail(Request $request)
{

}

How to Middleware work with route to super admin panel, admin panel, and user panel.

Leave a Reply

Your email address will not be published. Required fields are marked *