N
N
naneri2014-10-29 08:16:55
JavaScript
naneri, 2014-10-29 08:16:55

How to load parameters in JS on first page load?

It is necessary to transfer JSON with various parameters during the first load, how can this be done?
I use Laravel - when I pass a json_encode object, then it can’t properly assign it (for some reason, all characters are immediately escaped).
(AJAX request immediately after loading exclude)

foreach($allImages as $image){
      $arr[] = $image->__toString();
    }
    $json_arr = json_encode($arr);
    return view('index', ['images' => $arr, 'json_images' => $json_arr]);

This is how the code is passed to the view, and there it is simply displayed using console.log in the browser console.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ajaxtelamonid, 2014-10-29
@ajaxtelamonid

You can put js-code in the view that assigns variables to window, and read them from there in javascript. There is such package for Laravel: https://github.com/laracasts/PHP-Vars-To-Js-Transformer

V
Vladimir Rodkin, 2014-10-29
@VovanR

Are you parsing the incoming JSON https://developer.mozilla.org/en/docs/Web/JavaScri...
What does the escaped string look like?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question