0

I have a php page that queries a mysql db and return a .json file.

I want this page to be called within a coffeescript page, but I don't understand the syntax to accomplish this.

I have seen here that in plain js you can do:

var json = eval(<? echo $json ?>);   

but this is not a valid coffeescript syntax... is there a coffeescript alternative to eval() I can use? Or is there any alternative way I can go?

Community
  • 1
  • 1
user299791
  • 2,021
  • 3
  • 31
  • 57
  • As pointed out in this question http://stackoverflow.com/questions/14010133/how-to-grab-data-from-json-in-coffeescript you can use JSON.parse(json) to grab the json string and convert it into a json object. – LordNeo Feb 16 '17 at 15:36

1 Answers1

0

Yo do not need eval to parse a string of JSON into an object. It should be enough to use JSON.parse.

json = JSON.parse(<?php echo $json; ?>)
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • thanks, but the problem is stating this from coffeescript and as far as I got, you cannot use "var"... am I wrong? – user299791 Feb 16 '17 at 13:50
  • @user299791 I am not too familiar with coffeescript, but as long as you parse the content of your JSON file it should be accessible like any other object in coffeescript afterwards. – OptimusCrime Feb 16 '17 at 13:52
  • I need to call this php page every time something changes in the UI and the UI is managed with coffeescript... that's why I have this precise question... – user299791 Feb 16 '17 at 13:55
  • 1
    just do `data = JSON.parse(json)` other than the absence of "var" there isn't much difference. – LordNeo Feb 16 '17 at 15:37