如何测试返回的json结果?
16lz
2021-01-22
I have an Ajax Request that is getting data from my database, specifically two columns from it, company_id and people_id, I need to test the result that comes from the json result, I need to check if a specific people_id exists in the result,
我有一个从我的数据库获取数据的Ajax请求,特别是它的两列,company_id和people_id,我需要测试来自json结果的结果,我需要检查结果中是否存在特定的people_id,
Here is my Ajax Request
这是我的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 () {
}
});
I thought I could do this following code in the success function but it didnt work
我认为我可以在成功函数中执行以下代码,但它不起作用
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.');
}
Also here is my php
这也是我的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 个解决方案
#1
1
I think something like this should work:
我认为这样的事情应该有效:
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;
}
}
}
},
更多相关文章
- 从函数返回2个值[duplicate]
- PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测
- PHP递归函数删除所有子节点导致stackoverflow
- 使用 PHP usort() 通过用户自定义的比较函数对数组进行排序
- [开心学php100天]第五天:string函数(上)
- curl获取网页内容出现乱码或为空的解决方案,另附curl_getinfo函数
- APMServ 在 Win7 下出现“APMServ-Apache 服务因 函数不正确。
- 本机PHP函数将授予我直接访问字符串部分而无需创建临时数组的权
- php的冷门函数之——call_user_func_array,func_get_args,func_n