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

时间:2021-04-21 20:17:45

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. But I need the SQL results.

这是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
}

1 个解决方案

#1


4  

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.

看起来你的方法actionFamily返回DataProvider对象而不是你希望它获取的数据。如果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功能)。

#1


4  

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.

看起来你的方法actionFamily返回DataProvider对象而不是你希望它获取的数据。如果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功能)。