简体   繁体   中英

Yii2 and Ajax: The response is not the SQL query results

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM