未捕获的引用错误:.. 未定义 .. 但我可以在错误字符串中看到该变量 [英] Uncaught reference error: .. is not defined .. but I can see the variable in the error string
问题描述
这行代码 var $sTest = <?php echo $a = $_GET['ID']?>
返回未捕获的引用错误:.. 未定义."
This line of code var $sTest = <?php echo $a = $_GET['ID']?>
returns a "Uncaught reference error: .. is not defined."
对于这个问题,如果我们假设我传递的是ABC"的 ID.当我在 Chrome 中使用开发人员工具并检查资源时,我可以看到 $_GET 已 起作用,因为它向我显示了变量值.这种情况下的错误消息是未捕获的引用错误:未定义 ABC."
For this question, if we assume I am passing an ID of "ABC." When I use the Developer Tools in Chrome and inspect the Resources, I can see that the $_GET has worked because it shows me the variable value. The error message in this case is "Uncaught reference error: ABC is not defined."
我不知道如何将 php 变量分配给脚本变量.感谢帮助.
I can't work out how to assign the php variable to a script var. Help appreciated.
推荐答案
这是因为你必须引用你的 php 脚本的输出:
It's because you have to quote your php script's output:
var $sTest = <?php echo $a = $_GET['ID']?>
将成为
var $sTest = something;
在您的 Javascript 环境中,something
未定义.如果你想保留尽可能多的类型信息,你应该使用 json_encode 在 php 端是这样的:
And in your Javascript enviroment the something
is not defined. If you want to preserve as much type information as you can, you should use json_encode on the php side like this:
var $sTest = <?php print json_encode($_GET['ID']);?>;
这篇关于未捕获的引用错误:.. 未定义 .. 但我可以在错误字符串中看到该变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!