从文本文件中提取JSON数据并将其放在javascript中的数组中

时间:2021-08-04 17:49:53

i have a text file named "vars.txt" that holds an array. How can i pull that information and put it in a javascript array? right now i have

我有一个名为“vars.txt”的文本文件,其中包含一个数组。我如何提取该信息并将其放入javascript数组中?现在我有

<script type="text/javascript">
function test() {
  var testvar = <?php file_get_contents('vars.txt') ?>;
  alert ("success");
  alert (testvar);
};
</script>

and that is not working. is there a better way to pull this data into an array?

那是行不通的。有没有更好的方法将这些数据拉入数组?

2 个解决方案

#1


1  

<script type="text/javascript">
function test() {
  var testvar = <?php echo file_get_contents('vars.txt') ?>;
  alert ("success");
  alert (testvar);
};
</script>

You forgot to echo the data, without this nothing will be rendered into the javascript function.

你忘了回复数据,如果没有这些将被渲染到javascript函数中。

To debug situations like this, just view the source of the rendered webpage, and see what's actually printed.

要调试这样的情况,只需查看渲染网页的来源,然后查看实际打印的内容。

#2


0  

Use json_decode() to decode the JSON data from vars.txt:

使用json_decode()解码来自vars.txt的JSON数据:

var testvar = <?php echo json_decode(file_get_contents('vars.txt')) ?>;

#1


1  

<script type="text/javascript">
function test() {
  var testvar = <?php echo file_get_contents('vars.txt') ?>;
  alert ("success");
  alert (testvar);
};
</script>

You forgot to echo the data, without this nothing will be rendered into the javascript function.

你忘了回复数据,如果没有这些将被渲染到javascript函数中。

To debug situations like this, just view the source of the rendered webpage, and see what's actually printed.

要调试这样的情况,只需查看渲染网页的来源,然后查看实际打印的内容。

#2


0  

Use json_decode() to decode the JSON data from vars.txt:

使用json_decode()解码来自vars.txt的JSON数据:

var testvar = <?php echo json_decode(file_get_contents('vars.txt')) ?>;