简体   繁体   English

来自PHP的JSON数组在主JS文件中未定义编码

[英]JSON Array from PHP encode in undefined in main JS file

I have today a very strange problem with JS array. 我今天对JS数组有一个非常奇怪的问题。 First, I am encoding JSON array in php and echo an array: 首先,我在php中编码JSON数组并回显一个数组:

echo '<script type="text/javascript">var odpowiedzi = []; var odpowiedzi = '.json_encode($_SESSION['odpowiedzi']).'; alert(odpowiedzi);</script>';

What is interesting, I see here that alert with good data of my array but in 2nd JS file I am trying to see it again: 有趣的是,我在这里看到警报,其中包含阵列的良好数据,但是在第二个JS文件中,我试图再次看到它:

alert(odpowiedzi[0]);

And only thing I get is critical JS error of undefined: 我唯一得到的是未定义的严重JS错误:

VM5244:7 Uncaught TypeError: Cannot read property '0' of undefined

What is the problem? 问题是什么? For test I made same thing with normal int variable and it is working! 为了测试,我用普通的int变量做了同样的事情,它正在工作! Please for help. 请寻求帮助。

Looks to me like your second JS file is getting loaded first. 在我看来,您的第二个JS文件已被首先加载。 You can simply add console.log() commands and log the time when they get called to see if there's an issue with the order of your scripts getting loaded as such; 您可以简单地添加console.log()命令并记录调用它们的时间,以查看脚本的加载顺序是否存在问题。

console.log('nth block execute time: ' + new Date().getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM