mercredi 22 février 2017

How to pass JSON response from Controller back to AJAX success method in Laravel 5.4

I have a calling js script

setInterval(function(){

    $.get( "/fetch-data", function( data ) {
        console.log(data);
    });

},1000);

and my Routes look like this

Route::get('/fetch-data', 'PageController@fetchData');

my controller method

public function fetchData()
{
    $results_array = [];
    $file = new \SplFileObject('file.csv');
    $file->setFlags(\SplFileObject::READ_CSV);

    foreach ($file as $row) {
        $results_array[] = $row[0];
        $results_array[] = $row[1];
        $results_array[] = $row[2];
        $results_array[] = $row[3];
         ...
    }
     return response()->json($results_array);
     //$json = response()->json($results_array);
     //return view('view.blade',compact($json));    
}

Note that with return response()->json($results_array); the raw json text is displayed directly on the page. Not what I want.

While return view('view.blade',compact($json)); returns the whole html file. Still not what I want.

What I would like to achieve is that the raw json text that was returned from the controller be pass to the callback from the calling js script.

Any ideas?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2loWjWz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire