【问题标题】:Structure PHP array to pass to JS correctly构造 PHP 数组以正确传递给 JS
【发布时间】:2013-01-29 10:32:09
【问题描述】:

当我们这样做时,这会起作用:

$db = $connection->messages;
$collection = $db->messagesCollection;
$messageArray = $collection->find(array('IDto' => '4'));
foreach($messageArray as $messageData){
    $messageFrom = $messageData['IDfrom'];
    $messageTo = $messageData['IDto'];
    $messageTitle = $messageData['messageTitle'];
    $messageIfRead = $messageData['ifRead'];    
}

$JSONData = array('true', $messageFrom, $messageTo, $messageTitle, $messageIfRead); 
echo $_GET['callback']."(".json_encode($JSONData).")";

但是当我们这样做时:

$db = $connection->messages;
$collection = $db->messagesCollection;
$messageArray = $collection->find(array('IDto' => '4'));
$JSONData = array('true', $messageArray); 
echo $_GET['callback']."(".json_encode($JSONData).")";

在 Javascript 中这样做:

$.getJSON("mySite.com/pullData/getMail.php?callback=?",{
request: requestVar},
function(recievedData) {
    alert(recievedData);
})

我们收到true, [object Object] 的警报 使用控制台日志时,我们得到Object {}

我们如何正确发送该表数据?

【问题讨论】:

    标签: php javascript jquery json mongodb


    【解决方案1】:

    我相信你最大的问题是MongoCursor:

    $messageArray = $collection->find(array('IDto' => '4'));
    $JSONData = array('true', $messageArray); 
    echo $_GET['callback']."(".json_encode($JSONData).")";
    

    您正在尝试在那里编码MongoCursor 的对象,因此字符串表示为[object Object]

    试试:

    $messageArray = $collection->find(array('IDto' => '4'));
    $JSONData = array('true', iterator_to_array($messageArray)); 
    echo $_GET['callback']."(".json_encode($JSONData).")";
    

    相反。 MongoDB 中的所有find 函数都返回一个MongoCursor,您的第一个代码有效的原因是您迭代游标来构建您的对象:

    foreach($messageArray as $messageData){
        $messageFrom = $messageData['IDfrom'];
        $messageTo = $messageData['IDto'];
        $messageTitle = $messageData['messageTitle'];
        $messageIfRead = $messageData['ifRead'];    
    }
    

    还要注意,文档的默认json_encode 将包含对象,例如MongoIdMongoDate,它们不能很好地编码成可重用的JSON 语法。因此,您需要自己处理这些类型。

    编辑

    也许更好的方法是手动重做索引:

    $messageArray = $collection->find(array('IDto' => '4'));
    $d = array();
    foreach($messageArray as $row){
        $d = $row;
    }
    
    $JSONData = array('true', $d); 
    echo $_GET['callback']."(".json_encode($JSONData).")";
    

    这样您将拥有一个基于 0 的递增索引,而不是 ObjectId 作为每个索引的基础。

    【讨论】:

    • 好的,现在我将所有数据作为对象获取,如何从像 receivedData[1]['IDFrom'] 这样的数组键中的括号中获取它们?现在看起来像这样:i46.tinypic.com/2j5xdau.png
    • @Silas 你可以做的是(因为我注意到你使用 JQuery):$.each(recievedData, function(i, item){ console.log(item._id.$id); }); 你可能想手动提取索引,除非这些对象 id 索引在你的应用程序中正常工作。
    • 这太棒了!它开始起作用了。非常感谢您的时间。有没有办法我可以在 javascript/jquery 中“foreach”这个,所以我可以处理多行?
    • recievedData[1]['userIdTo'] 当我这样做时,我得到了一行的正确响应。如何获得通过的其他行?
    • @Silas Ha 更正了我给你的循环代码:$.each(recievedData[1], function(i, item){ console.log(item._id.$id); }); 应该为 var 访问索引一,因为数据实际上在位置 1
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 2018-09-25
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2018-02-02
    • 1970-01-01
    相关资源
    最近更新 更多