0

I need to figure out why i cannot get this Session to JS for a JSON object. I am sure i missing something simple.

I have a Session var being set in laravel

Session::put('test', "{'test1':'123','test2':'456'}");

I want to set this in a page in js :

<script>var testvar = "{{\Session::get('test')}}";

then i try and do this in js :

var test_json = JSON.parse(testvar);

but i am seeing the js testvar is set with the quotes encoded?

what i am i doing wrong???

5
  • 1
    why do you want to use Session in a blade? You need to pass these values as parameters to the blade, respecting MVC. Commented Nov 21, 2018 at 19:00
  • 1
    1. That's not JSON, 2. There's no such thing as a "JSON object" Commented Nov 21, 2018 at 19:00
  • @Andreas If it walks like a duck and talks like a duck, it's a json object. :)~ Commented Nov 21, 2018 at 19:06
  • @JustinSchwimmer If something is wrong, its wrong. Just because there's a site that's using the wrong term for it doesn't make it correct. Commented Nov 21, 2018 at 19:20
  • 1
    @Andreas If it's wrong - we have to right a wrong. And sometimes, in order to right a wrong, you have to do a wrong-right. Commented Nov 21, 2018 at 19:24

1 Answer 1

4

The problem

Your following code

<script>
   var testvar = "{{\Session::get('test')}}";
   var data = JSON.parse(testvar);
</script>

would be interpreted as:

<script>
   var testvar = {'test1':'123','test2':'456'};
   var data = JSON.parse(testvar);
</script>

{'test1':'123','test2':'456'} is invalid JSON, as you need to use double quotes instead of single quotes.

So you need:

Session::put('test', '{"test1":"123","test2":"456"}');

But this creates another problem.

Laravel auto escapes HTML for security reasons to avoid XSS attacks.

This means the above is now interpreted as :

<script>
    var testvar = {&quot;test1&quot;:&quot;123&quot;,&quot;test2&quot;:&quot;456&quot;}&quot;
    var data = JSON.parse(testvar);
</script>

If you are sure the data you are storing is safe, you can use raw tags like so, and avoid using JSON.parse (so we don't need to re-escape any of the quotes again) like so:

<script>
    var testvar = {!! Session::get('test') !!};
</script>

This will then output the HTML:

<script>
    var testvar = {"test1":"123", "test2":"456"};
</script>

A better solution

A better way is to store your data as an array in the PHP session:

Session::put('test', [
    'test1' => '123',
    'test2' => '456'
]);

Then you could just do:

<script>
    var testvar = @json(Session::get('test'));
</script>

This is explained here: https://laravel.com/docs/5.7/blade#displaying-data

Side Note

As a side note, consider passing a variable into blade to avoid doing logic in your views like so:

return view('your-view-name', [
    'testvar' => Session::get('test'),
]);

Then you could just do:

<script>
    var testvar = @json($testvar);
</script>
Sign up to request clarification or add additional context in comments.

12 Comments

The properties (and if required also the values) have to be wrapped in double quotes
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and use var testvar = @json(Session::get('test'));.
In your first version of the answer you were using "{!! Session::get('test') !!}" which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");)
|

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.