簡體   English   中英

Yii2和Ajax:響應不是SQL查詢結果

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

我想使用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 \\控制器,它只是按原樣返回對象。

要獲取DataProvider的數據,請嘗試更改此...

return $dataProvider;

進入這...

return $dataProvider->getModels();

或如上所述更改控制器類(如果它是REST功能)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM