我想使用 jQuery 执行 AJAX 查询,但响应不是我想要的。
客户端:
$.ajax({
url: "/family?idperson=1234",
dataType: 'json',
success: function(res) {
console.log(JSON.stringify(res, null, 4));
},
error: function(err) {
}
});
服务器端:
public function actionFamily($idperson)
{
$searchModelFamily = new FamilySearch();
$dataProvider = $searchModelFamily->searchByIdperson(Yii::$app->request->queryParams, $idperson); // This database query works great.
Yii::$app->response->format = Response::FORMAT_JSON;
return $dataProvider;
}
这是 JSON 对象的内容: 它似乎是 SQL 查询的某些部分。但我需要 SQL 结果。
{
"query": {
"sql": null,
"on": null,
"joinWith": null,
"select": null,
"selectOption": null,
"distinct": null,
"from": null,
"groupBy": null,
"join": null,
"having": null,
"union": null,
"params": [],
"where": {
"idperson": "1234"
},
"limit": null,
"offset": null,
"orderBy": null,
"indexBy": null,
"emulateExecution": false,
"modelClass": "app\\models\\Family",
"with": null,
"asArray": null,
"multiple": null,
"primaryModel": null,
"link": null,
"via": null,
"inverseOf": null
},
"key": null,
"db": null,
"id": null
}
最佳答案
您的方法 actionFamily 似乎返回 DataProvider 对象,而不是您希望它获取的数据。如果 actionFamily 是 yii\rest\controller 中的一个方法,它应该可以工作,但我的猜测是您使用的是常规 yii\web\controller,它将按原样返回对象。
要获取 DataProvider 的数据,请尝试更改此...
return $dataProvider;
进入这个...
return $dataProvider->getModels();
或如上所述更改 Controller 类(如果它是 REST 功能)。
关于javascript - Yii2 和 Ajax : The response is not the SQL query results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46199201/