注意注意: 本文对应 ThinkPHP5.1 版本。
前言 - 出现问题的原因
为避免出现 XSS 安全问题,Thinkphp5.1 默认变量输出都会使用 htmlentities 方法进行转义输出。
如果不想被转义输出,模板渲染时,需要在变量后面加上 raw方法,如:{$data|raw}
一、出现问题前的代码
1.1 PHP 代码
# thinkphp 框架向前台输出变量 $this->assign('store', $info);
1.2 前端代码
let data = "{$store}";
1.3 报错
问题描述: htmlentities() expects parameter 1 to be string, array given
二、解决问题后的代码
2.1 PHP 代码
<?php # thinkphp 框架向前台输出变量, 转为 json 字符串 $this->assign('store', json_encode($info));
2.2 前端代码
// javascript,加上 `raw` 方法 let data = JSON.parse('{$store|raw}'); // 这样就被转化成一个对象 // 就可以愉快的循环了 $.each(store_info , function(key , val){ let name = val.name; })
- htmlentities Thinkphp5 parameter Thinkphp expectshtmlentities thinkphp5 parameter thinkphp thinkphp5 thinkphp5 weblogic thinkphp thinkphp5框架thinkphp数据库 thinkphp5 thinkphp接口 数据 漏洞分析 漏洞thinkphp5 thinkphp thinkphp5 thinkphp paginate日期 thinkphp thinkphp3 thinkphp5常用 postgresql thinkphp5 thinkphp model thinkphp5 thinkphp sdk