Laravel 5.1: Ajax data not accepted from server

The route is shown below:

$router->get('/securityquestionlist', [ 'as'=> 'SecurityQuestionListIndexRoute', 'uses'=> 'SecurityQuestionListController@index']);

I have the following action in the controller class:

public function index()
{
    $model = new SecurityQuestionListModel();

    $data = $model->select('question','created_at', 'updated_at', 'status')->where('status', 1)
            ->orderBy('created_at', 'desc')
            ->paginate(3);

if(Request::ajax()){
        return response()->json(['rData' => $data]);
    }else{
        return view('securityquestionlist.index' /* ,['rData'=> $data]*/ );
    }

Below is the Ajax code:

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#IdSQLTable').DataTable({
            'ajax'  : 'http://localhost:9901/securityquestionlist',
            'cache' : false

        });
    } );
</script>

I get the following AJAX response from the server:

{"rData":{}}

Can someone help me, in the case of AJAX, why the value of $ data is not returned from the server. If I disable ajax and load a regular page, then the value will be received on the client side, and the data will be filled in the rows of the table. Now I have done it in:

return view('securityquestionlist.index' /* ,['rData'=> $data]*/ );
+4
source share
1 answer

Think you need to convert an array datato an array withtoArray()

$rData = $data->toArray();

Reply reply using

return response()->json(['rData' => $rData]);
+2
source

Source: https://habr.com/ru/post/1614568/


All Articles