[" 6 "," 1 "," 2-6ren">
gpt4 book ai didi

javascript - 语法错误: unterminated string literal with JSON array

转载 作者:行者123 更新时间:2023-11-28 04:19:54 25 4
gpt4 key购买 nike

所以我有...

 var newfavz = 'Array (


[0] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]
[1] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]
[2] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]

)
';

在我的 console.log 上,但不知何故存在语法错误:未终止的字符串文字。我环顾四周并尝试了诸如 str_replace "/"与 "//"之类的方法或像 .replace(/^/+/g, ''); 这样的正则表达式;因为 JavaScript 似乎不允许将字符串分成多行或类似的东西。

这一切都是从像这样的 SQL 查询开始的......

$favurl = [];
$favquery = "SELECT * FROM userfavs WHERE users = '$username'";
$favresult = mysqli_query($conn, $favquery);

while($row = mysqli_fetch_assoc($favresult)) {

array_push($favurl, $row['fav_id']);

后来我就这么做了

var newfavz = <?php print_r ($favurl); ?>

这导致了上述情况。

有什么办法可以解决语法错误吗?谢谢!

最佳答案

因为js中不允许多行字符串:

"A
B"

是一个语法错误。您可以删除所有换行符并将其替换为\n,或者使用模板文字:

`A
B`

在您的代码中:

var newfavz =` <?php print_r ($favurl); ?>`;

关于错误就这么多。但是该字符串仍然无法使用,需要对其进行解析。查看 JSON 或编写您自己的小解析器。

关于javascript - 语法错误: unterminated string literal with JSON array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45526316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com