Home:ALL Converter>How to store JSON response from controller in a Javascript variable

How to store JSON response from controller in a Javascript variable

Ask Time:2019-01-11T14:28:08         Author:user2018756

Json Formatter

Problems storing json sent from laravel controller into a javascript variable in the View.

The controller code

public function list() {
    $rst=\App\Emp::all();
    $json_rsp=response()->json($rst);
    return view('emp')->with('json',$json);
}

In my View I am trying to store the response in a JavaScript variable

var data={{ $json }};

View-> Source the variable contains

HTTP/1.0 200 OK
Cache-Control: no-cache, private
Content-Type:  application/json
Date:          Fri, 11 Jan 2019 06:10:50 GMT
{"id":1,"e_name":"Kate","type":"Mixed","dof":"2004-12-12" ETC ETC

Parsing this data using JSON.parse throws an error.

I expect to store pure json in javascript variable to put it to further use but as the response has headers the input is not proper JSON. Is there any way to send JSON data from Controller to the view without the headers? I am using Laravel 5.7 by the way.

Author:user2018756,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54141396/how-to-store-json-response-from-controller-in-a-javascript-variable
yy