2012-02-24 226 views
1

不知道哪裏可能出錯了代碼對我來說看起來很好,我知道file.php - > mod_rewtite - > file.json的工作原理,因爲我測試了來自此文件的JSON響應通過jQuery。在任何情況下,下面的代碼位都會立即觸發錯誤響應,而不是事實JSON請求爲什麼會猜測?dojo ajax請求不返回json數據

圖片的標題說明:我公司採用最新的Dojo Toolkit的1.7.1從Yandex的鏡子: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 - > 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 . ')'; 

?> 

回答

0

完美的作品在URL中,你取file.json我不懷疑它 - 這似乎是錯誤的 應該真的是file.php?

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

也確保路徑是正確的 - 你可以檢查螢火Net標籤來查看請求它試圖發送