dojo ajax请求不返回json数据

时间:2012-02-24 17:01:55

标签: php ajax json dojo

不确定我可能出错的地方代码对我来说似乎很好,我知道file.php - > mod_rewtite - > file.json工作,因为我已经通过jQuery测试了这个文件的JSON响应。在任何情况下,附加的以下代码位会立即触发错误响应,而不是为了实现JSON请求的任何猜测?

旁注:我正在使用Yandex镜像中的最新Dojo Toolkit 1.7.1:http://yandex.st/dojo/1.7.1/dojo/dojo.js

这里是JSON的代码不起作用

<script type="text/javascript">
    require(["dojo/_base/xhr", "dojo/dom", "dojo/_base/array", "dojo/domReady!"],
    function(xhr, dom, arrayUtil) {

        // Keep hold of the container node
        var containerNode = dom.byId("content");

        // Using xhr.get, as we simply want to retrieve information
        xhr.get({
            // The URL of the request
            url: "file.json?path=<?php echo $_GET['path']; ?>&callback=?",
            // Handle the result as JSON data
            handleAs: "json",
            // The success handler
            load: function(jsonData) {
                // Create a local var to append content to
                var html = "";
                // For every news item we received...
                arrayUtil.forEach(jsonData, function(item) {
                    // Build data from the JSON 
                    html += "<div class='product' data='" + item.path + "'>";
                    html += "<div class='like'></div>";
                    html += "<img src='thumb.php?q=100&w=298&h=238&a=t&src='" + item.thumb + "' width='298' height='238'/>";
                    html += "<div class='title'>" + item.name + "</div>";
                    html += "<div class='description'></div>&nbsp;<strong>Filesize:</strong>" + item.size + "<div style='clear:both;height:8px;'></div>&nbsp;about" + item.date + " ago<div style='clear:both;height:8px;'></div></div><div class='clear'></div></div>";
                });
                // Set the content of the news node
                containerNode.innerHTML = html;
            },
            // The error handler
            error: function() {
                containerNode.innerHTML = "News is not available at this time."
            }
        }); 
});
</script>

这里还有file.php - &gt; file.json代码位甚至你需要它,但正如我所说,我不怀疑它,因为它与jQuery完美配合

<?php

$dir = $_GET['path'] . "/";

function time_since($since) {
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
        array(1 , 'second')
    );

    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    return $print;
}

$dh = opendir($dir);
$files = array();
while (($file = readdir($dh)) !== false) {
    if ($file != '.' AND $file != '..' ) {
        if (filetype($dir . $file) == 'file') {
            $files[] = array(
                'id' => md5($file),
                'name' => htmlentities(md5($file)),
                'size' => filesize($dir . $file). ' bytes',
                'date' => time_since(date("ymd Hi", filemtime($dir . $file))),
                'path' => $dir . $file,
                'thumb' => $dir . 'small/' . $file
            );
        }            
    }
}
closedir($dh);

    $json = json_encode($files);

    $callback = $_GET['callback'];
    echo $callback.'('. $json . ')';

?>

1 个答案:

答案 0 :(得分:0)

在url中,你正在获取file.json - 这似乎是错误的 那真的应该是file.php吗?

url: "file.php?path=<?php echo $_GET['path']; ?>&callback=?",

还要确保路径正确 - 您可以检查firebug中的Net选项卡以查看它尝试发送的请求