Afshin Izadi
Afshin Izadi

Reputation: 526

How to use laravel respose json in javascript

in my controller

return view('admin.venue', ['cityList' => City::all()->toJson() ]);

and also i have tried it with

in my view

var allCities =  {{$cityList}};

and also i have tried

var allCities = JSON.parse({{$cityList}})
and
var allCities = JSON.parse('{{$cityList}}')

but it reply below error , i think the problem is about double quot. what is the correct way to use laravel json response in javascript

SyntaxError: invalid property id

var allCities = [{&-quot;id":1,&-quot;name":"cityname&-quot;,&-quot;p

Upvotes: 1

Views: 92

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

Try to use {!! !!} instead of {{ }}:

var allCities =  {!! $cityList !!};

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}

https://laravel.com/docs/5.3/blade#displaying-data

Upvotes: 2

Related Questions