>我想使用查询来返回集合的全部内容。但是,当我使用 PHP
搜索被 $.ajax()
绊倒时,它会返回ObjectId
为 [object Object]
。我正在尝试将其作为字符串获取。我该如何继续?
按照$.ajax()
代码:
$.ajax({
url: 'funcoes/registroeventos.php',
data: {
"ref": ref
},
type: 'post',
dataType: 'json',
cache: false,
beforeSend: function (xhr) {
},
error: function (jqXHR, textStatus, errorThrown) {
},
success: function (dados) {
$.each(dados, function () {
$.each(this, function (index, value) {
alert(value);
});
});
}
});
然后,PHP
代码:
$ref = $_POST['ref'];
try {
$consulta = ['ref' => $ref, 'excluido' => 'n'];
$opcoes = [];
$query = new MongoDBDriverQuery($consulta, $opcoes);
$linhas = $conexao->executeQuery($bd . "maquinas", $query);
echo json_encode(iterator_to_array($linhas));
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
可以使用以下脚本获取值。
$.ajax({
url: 'funcoes/registroeventos.php',
data: {
"ref": ref
},
type: 'post',
dataType: 'json',
cache: false,
beforeSend: function (xhr) {
},
error: function (jqXHR, textStatus, errorThrown) {
},
success: function (dados) {
$.each(dados, function (k,v) {
alert(v.your_value);
});
}
});
您需要循环响应,然后您可以使用v.data, v.message
等检索值...
读完我的json就很清楚了。
[
{"_id":
{"$oid":"5aafac02dc32b7f93a3fda00"},
"ref":"DIP001",
"nome":"Dip Tork",
"status":"setup",
"excluido":"n",
"idsetor":"5aafaba2dc32b7f93a3fd9ff"}
]
所以,一个刚刚做到了:alert(dados._id.$oid);
而不是alert(dados._id);