简体   繁体   English

Yii2和Ajax:响应不是SQL查询结果

[英]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. 我想使用jQuery执行AJAX查询,但响应不是我想要的。

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. 这是JSON对象的内容:它似乎是SQL查询的某些部分。 But I need the SQL results . 但我需要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
}

It seems like your method actionFamily returns the DataProvider object rather then the data you want it to fetch. 看起来你的方法actionFamily返回DataProvider对象而不是你希望它获取的数据。 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. 如果actionFamily是yii \\ rest \\ controller中的一个方法它应该可以工作,但我的猜测是你使用的是常规的yii \\ web \\控制器,它只是按原样返回对象。

To get the data of the DataProvider, try changing this... 要获取DataProvider的数据,请尝试更改此...

return $dataProvider;

into this... 进入这...

return $dataProvider->getModels();

or change the controller class (if it is a REST feature) as discussed above. 或如上所述更改控制器类(如果它是REST功能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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