Not sure where I might have went wrong the code seems fine to me, and I know the file.php -> mod_rewtite -> file.json works as I have tested JSON response from this file via jQuery. In any event the following code bit attached triggers the error response right away instead of going truth the JSON request any guesses why?
sidenotes: I am using latest Dojo Toolkit 1.7.1 from Yandex mirror: http://yandex.st/dojo/1.7.1/dojo/dojo.js
and here is the code for the JSON which does not work
<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> <strong>Filesize:</strong>" + item.size + "<div style='clear:both;height:8px;'></div> 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>
Also here is the file.php -> file.json code bit in even you need it, but as I said I do not suspect it as it works perfectly with 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 . ')';
?>
In the url, you are fetching file.json – that seems to be erroneous
Should that really be file.php?
also make sure the path is correct – you can examine the Net tab in firebug to see the request it is trying to send