Javascript 将php数组作为JSON传递给js会导致参数列表后出现未捕获的语法错误(错误:missing)
我试图将php数组传递给ajax 当我把它传递到onclick函数中时,我得到了未捕获的SyntaxError:missing)after参数列表 当我只更改变量$module时,错误消失 PHP代码:Javascript 将php数组作为JSON传递给js会导致参数列表后出现未捕获的语法错误(错误:missing),javascript,php,json,Javascript,Php,Json,我试图将php数组传递给ajax 当我把它传递到onclick函数中时,我得到了未捕获的SyntaxError:missing)after参数列表 当我只更改变量$module时,错误消失 PHP代码: <?php foreach($this->data AS $module_name => $module): if ($module_name == "Descriptions") {
<?php foreach($this->data AS $module_name => $module):
if ($module_name == "Descriptions") {
continue;
}
?>
<table>
<thead>
<tr>
<th onclick="loadModule('<?=$module_name?>', '<?=htmlspecialchars(json_encode($module))?>', '<?=json_encode($this)?>')" data-module="<?=$module_name?>" colspan="<?php echo count($this->langs) + 2; ?>"><?= $module_name; ?>
我不擅长设置这些问题的样式,对此我深表歉意。一种方法是将base 64编码的字符串传递给html,而不是使用htmlspecialchars。注意:您还需要对第三个参数进行base64编码
<th onclick="loadModule('<?=$module_name?>', '<?=base64_encode(json_encode($module))?>', '<?=base64_encode(json_encode($this))?>')" data-module="<?=$module_name?>" colspan="<?php echo count($this->langs) + 2; ?>"><?= $module_name; ?>
你为什么这么做<代码>htmlspecialchars(json_encode($module))?这将更改JSON字符串,并可能使其成为无效JSON。如果模块需要它,您应该
json\u encode(htmlspecialchars($module))
。如果错误发生在客户端,那么您需要做的第一件事是检查客户端代码,而不是检查生成客户端代码的服务器端代码。找出代码结果中的错误,这将帮助您确定可能需要更改的内容。$module包含“,”和其他html标记,如果我不使用htmlspecialchars和json编码,数组将被打印到屏幕上,因为它结束了onclick和thWhat,这是
和
的要点,而$module
中包含的内容已经是$this
的一部分了?(您首先在这里循环$this->data
来填充$module
)为什么需要这些冗余信息?JavaScript级别的语法问题:您知道foo([“bar”,“baz”])
和foo(“[“bar”,“baz”])
之间的区别吗…?
> array (size=2)
'names' =>
array (size=7)
'name' => string 'Names' (length=5)
'table_name' => string 'dictionary_words_names' (length=22)
'type' => int 2
'cols' => null
'key' => string 'dictionary_word_name_term' (length=25)
'col_prefix' => string 'dictionary_word_name_' (length=21)
'data' =>
array (size=7)
'en' => string 'The query to database has failed. Unknown column 'dictionary_word_name_en' in 'field list'' (length=90)
'cs' =>
array (size=275)
...
'de' =>
array (size=275)
...
'pl' =>
array (size=275)
...
'it' => string 'The query to database has failed. Unknown column 'dictionary_word_name_it' in 'field list'' (length=90)
'fr' => string 'The query to database has failed. Unknown column 'dictionary_word_name_fr' in 'field list'' (length=90)
'hu' => string 'The query to database has failed. Unknown column 'dictionary_word_name_hu' in 'field list'' (length=90)
'descriptions' =>
array (size=7)
'name' => string 'Descriptions' (length=12)
'table_name' => string 'dictionary_words_descriptions' (length=29)
'type' => int 2
'cols' => null
'key' => string 'dictionary_word_description_term' (length=32)
'col_prefix' => string 'dictionary_word_description_' (length=28)
'data' =>
array (size=7)
'en' => string 'The query to database has failed. Unknown column 'dictionary_word_description_en' in 'field list'' (length=97)
'cs' =>
array (size=275)
...
'de' =>
array (size=275)
...
'pl' =>
array (size=275)
...
'it' => string 'The query to database has failed. Unknown column 'dictionary_word_description_it' in 'field list'' (length=97)
'fr' => string 'The query to database has failed. Unknown column 'dictionary_word_description_fr' in 'field list'' (length=97)
'hu' => string 'The query to database has failed. Unknown column 'dictionary_word_description_hu' in 'field list'' (length=97)
<th onclick="loadModule('<?=$module_name?>', '<?=base64_encode(json_encode($module))?>', '<?=base64_encode(json_encode($this))?>')" data-module="<?=$module_name?>" colspan="<?php echo count($this->langs) + 2; ?>"><?= $module_name; ?>
module = JSON.parse(atob(module));
thisObj = JSON.parse(atob(thisObj));