Yii2 “Response content must not be an array” 报错解决方法
在Yii2中直接return数组、JSON、XML等格式时,会提示“Response content must not be an array” 报错。
具体解决方法如下:
问题代码
/**
* 获取省份
*/
public function actionGetprovince() {
$service = new Addressservice();
$province_list = $service->getProvinceList();
return $province_list;
}
/**
* 获取省份
*/
public function getProvinceList() {
$data = Province::find()->all();
return $data;
}报错原因:yii2默认Response Format为html格式,返回json等格式需格式数据。
解决方法
方法一
如果是单独某个方法需要返回json或其他格式数据,直接在方法return数据前加上:\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
/**
* 获取省份
*/
public function actionGetprovince() {
$service = new Addressservice();
$province_list = $service->getProvinceList();
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;// 返回json格式数据
return $province_list;
}方法二
修改配置文件\backend\config\main.php(这里是advanced版本,basic版是web.php文件),注意,所有控制器返回的数据格式都会按照这里的格式:
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
],#PHP #Yii
本文标题:Yii2 “Response content must not be an array” 报错解决方法
本文链接:https://www.befun.ink/detail/34.html
声明:本站信息原创或由互联网收集,未用于商业用途,如若侵权,请联系站长删除!
懒师傅敲代码
优秀作者 战斗力十足
1.9w
文章
312w+
阅读
635w+
访问量
相关文章