Skip to content
Advertisement

Parse Laravel JSON object in Javascript throwing error

Laravel code :

JavaScript

Then in client side I tried :

JavaScript

In webconsole I get :

JavaScript

In firefox debugger :

enter image description here

Advertisement

Answer

Use single quotes instead var teachers = '<?= json_encode($teachers) ?>';

or don’t use quotes at all, leaving out JSON.parse(teachers);.

JavaScript

Though you must check $teachers is valid, string, null or array at all times.

Else it would become var teachers = ; and break.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement