Pass parameters to Javascript

#1

Hi all,

I try to pass data from the server (for example, all cars from the database) to javascript to use this data. Now I pass the data to a template and in the template, I convert this data to json ( @play.libs.Json.toJson(carsList)) but in the code of the page, you can see all data and that is not good.

How can I pass data correctly?

Thanks

(Patrick Li) #2

Normally you would use AJAX to get your data from backend in frontend, especially if it is to be consumed and processed via JavaScript.

However, if you do/can not use AJAX, then perhaps you can store the data in a cookie or session (but you have to turn off the http-only flag so it is accessible via JavaScript) after you have parsed it into JSON, and in your JavaScript, you retrieve the cookie with the value. This is not safer than what you currently have, as cookies can be viewed by people that know how, but gets the job done if you simply do not want the car list to be shown in the HTML itself.

1 Like