Datatable icon indicating copy to clipboard operation
Datatable copied to clipboard

JSON formatting error

Open sithuaung opened this issue 11 years ago • 9 comments

When I try to initialize Datatable, I got something like that. error-message

and my json response is like that. json-response

And do I still need to call from the view like

$(document).ready(function() {
    $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/api.users"
    } );
} );

My controller

public function getDatatable()
    {
        return Datatable::collection(User::all(['name','nrc','company','email','phone','status']))
        ->showColumns('name','nrc','company','email','phone','status')
        ->searchColumns('name','nrc','company','email','phone','status')
        ->orderColumns('name','nrc','company','email','phone','status')
        ->make();
    }

My view

{{ Datatable::table()
    ->addColumn('Name','Nrc', 'Company','Email','Phone','Status')      
    ->setUrl(route('api.users'))   
    ->render() }}

My datatable version is 1.9.4. Can anyone help me? I've tried to searh similar questions like me with no luck. Thanks in advance.

sithuaung avatar Jan 14 '15 10:01 sithuaung

Looks like you have a dd or var_dump inside your code. You should remove that to get a pure json response

Chumper avatar Jan 14 '15 11:01 Chumper

Dear Chumper

No I don't have any dd or var_dump. I'll show you complete code.

My Route

Route::get('datatable', function(){
    return View::make('datatable');
});

Route::get('/api/users',[
    'as' => 'api.users',
    'uses' => 'HomeController@getDatatable'
]);

My Controller

public function getDatatable()
{
     return Datatable::collection(User::all(['name','nrc','company','email','phone','status']))
        ->showColumns('name','nrc','company','email','phone','status')
        ->searchColumns('name','nrc','company','email','phone','status')
        ->orderColumns('name','nrc','company','email','phone','status')
        ->make();
}

My View

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Table</title>
    {{ HTML::style('css/bootstrap.css') }}
    {{ HTML::style('css/dataTable.bootstrap.css') }}
    {{ HTML::script('js/jquery.js') }}
    {{ HTML::script('js/bootstrap.js') }}
    {{ HTML::script('js/jquery.dataTables.min.js') }}
</head>
<body>

<div class="container">

 {{ Datatable::table()
    ->addColumn('Name','Nrc', 'Company','Email','Phone','Status')      
    ->setUrl(route('api.users'))   
    ->render() }}

</div>


</body>
</html>

That's it.I reference the necessary files before datatable called like you said.

sithuaung avatar Jan 15 '15 02:01 sithuaung

Anybody please...

sithuaung avatar Jan 16 '15 11:01 sithuaung

my tip would now to step through the code, as you cann see in the screenshot zour first lines lists the sql command. So somewhere there is a dd or var_dump on this sql command

Chumper avatar Jan 16 '15 12:01 Chumper

To @webcrazy

public function getDatatable()
{
     die();
}

And check again json. if Empty you try:

public function getDatatable()
{
    dd(User::all(['name','nrc','company','email','phone','status']));
}

What you see???

thienhung1989 avatar Jan 16 '15 17:01 thienhung1989

Dear @Chumper I'm pretty sure I don't have any dd or var_dump but my controller code.

sithuaung avatar Jan 17 '15 03:01 sithuaung

Dear @thienhung1989

With your first code, my Json response is

This request has no response data available.

with this second code json-response-with-dd

and error pop-up as well.

DataTables warning (table id='datatable': Data Tables warning: JSON data from server could not be parsed.This is caused by a JSON formatting error.

sithuaung avatar Jan 17 '15 03:01 sithuaung

@webcrazy You check again Model "User". First line: Invaild. And if All ok, you try with Model orther by 'Method two'. If first line sample with Model 'User:All', i sure have problem in Eloquent (modified).

Good Luck!

thienhung1989 avatar Jan 17 '15 05:01 thienhung1989

@thienhung1989 @Chumper Thank you guys. I'll try.

sithuaung avatar Jan 19 '15 08:01 sithuaung