如何测试返回的json结果?

时间:2016-03-30 12:19:36

标签: php json extjs

我有一个从我的数据库获取数据的Ajax请求,特别是它的两列,company_id和people_id, 我需要测试来自json结果的结果,我需要检查结果中是否存在特定的people_id,

这是我的Ajax请求

Ext.Ajax.request({
    url: 'system/index.php',
    method: 'POST',
    params: {
        class: 'CompanyPeople',
        method: 'secget',
        data: Ext.encode({
            people_id: Ext.getCmp('peopleGrid').selectedRecord.data.people_id
        })
    },
    success: function (response) {

    },
    failure: function () {

    }
});

我以为我可以在成功函数中执行以下代码,但它没有工作

if (data == 0) {
    Ext.MessageBox.alert('Status', 'Person is not attached to anything bud!.');
}
else {
    Ext.MessageBox.alert('Status', 'Person already attached to another company.');
}

这也是我的php

class CompanyPeople extends Connect {

    function __construct() {
        parent::__construct();
    }

    public function secget($vars) {
        $sql = "SELECT * FROM CompanyPeople where people_id={$vars->data->people_id}";

        $data = array();

        $total = 0;
        if ($result = $vars->db->query($sql)) {
            while ($row = $result->fetch_assoc()) {
                $data[] = array("people_id" => intval($row["people_id"]));
                $total++;
            }
            $result->free();
        }

        echo json_encode(array("success" => true, "total" => $total, "topics" => $data));
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这样的事情应该有效:

success: function( response ){
    if(response.success === true) {
        var personExist = false;

        for(var i = 0; i < response.topics.lenght; ++i) {
            // Your specific people_id in myValue
            if(response.topics[i]['people_id'] === myValue) {
                personExist = true;
                break;
            }
        }
    }
},