Reputation: 2265
I want to execute a AJAX query using jQuery but the response is not what I want.
Client side:
$.ajax({
url: "/family?idperson=1234",
dataType: 'json',
success: function(res) {
console.log(JSON.stringify(res, null, 4));
},
error: function(err) {
}
});
Server side:
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;
}
This is the content of the JSON object: It seems to a some parts of the SQL query. But I need the SQL results.
{
"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
}
Upvotes: 4
Views: 402
Reputation: 900
It seems like your method actionFamily returns the DataProvider object rather then the data you want it to fetch. If actionFamily is a method within a yii\rest\controller it should work, but my guess is that you are using a regular yii\web\controller that will just return the object as it is.
To get the data of the DataProvider, try changing this...
return $dataProvider;
into this...
return $dataProvider->getModels();
or change the controller class (if it is a REST feature) as discussed above.
Upvotes: 4