1

My laravel controller is returning this data on the blade view using this command {{$employees}}

It gives me:

[{"id":"1","name":"june"},{"id":"2","name":"joan"}]

In my JavaScript:

<script>

var jsonData = $.parseJson('{{$employees}}');

alert(1);

</script>

But somehow it's throwing an error because the alert is not displaying.

Whats the workaround on these?

4
  • Are you populating data with ajax? Commented Sep 29, 2016 at 1:18
  • Is there an error in the Javascript console? Commented Sep 29, 2016 at 1:27
  • Why not just use var jsonData = {{$employees}};? I don't know anything about laravel, but that should just output your data as a JavaScript array of objects, ready to use, no parsing necessary... Commented Sep 29, 2016 at 1:33
  • Thanks Barmar, yes it was throwing an error before. the cause quote was escape. Commented Sep 29, 2016 at 11:29

1 Answer 1

1

It is possible you need to use the tags that don't escape the data

{!! $employees !!}

instead of

{{ $employees }}

Is the example string that you supplied the exact string that's being output in the template. There could also be a ' in the content you are outputting which means it would end the string prematurely.

eg json = 'I can**'**t find the error'; 

The above would cause errors as the ' in the word can't would make it think the string ended

Sign up to request clarification or add additional context in comments.

2 Comments

{!! $employees !!} did the trick. What happen is that " was escape.
glad i could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.