mercredi 23 septembre 2015

Load Datatables with Json Object as source come from API on Laravel 5.1

i am making a datatables which have datsource come from my API, this API will return the JSON and return to view (jquery) in order to load on datatables. But i don't know why the datatables ajax is automatically add params like bellow to the url, so the website report to me the 404 error. How can i fix this?, this is my code to call ajax to the controller route.

 <script>

      $("#example1").DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "/getAllStaff/1",
        "columns": [
            { "data": "UserCode" },
            { "data": "FullNameSta" },
            { "data": "EmailSta" },
            { "data": "PhoneNumberSta" },
            { "data": "UserRoll" },
            { "data": "Status" },
            { "data": "Status" }
        ]
    });
</script>

And this is my route (call to controller to return data to ajax call):

Route::get('/getAllStaff/{page}',['as' => 'Value', 'uses' =>  'admin\adminFunctionController@getDisplayInfor']);

Here is my controller code:

 public function getDisplayInfor($page)
{

    $params = ''.$page;
    $action = 'getAllStaff';

    $result = ApiController::getAPI($params, $action);
    try{
        if($result->{'success'}){
            return response()->json(['recordsTotal'=>$result->{'CountData'}, 'recordsFiltered'=>$result->{'CountData'}, 'data'=>$result->{'data'}]);
        }else{
            return response()->json(['success'=>false, 'error'=>$result->{'error'}]);
        }
    }catch (\Exception $ex){
        return response()->json(['success'=>false, 'error'=>$ex]);
    }
}

So i was tried to get it on postman and it's working fine, but it isn't when i call by ajax cause the datatables auto add params to the url and more than that, when i tried to call many time to the ajax i see that i have some request that return data to me. That's weird, if the route is the root of the problem i don't think it can return data in some request!.

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire