2012-02-24 3 views
1

코드가 잘못되었을 수도 있습니다.이 파일에서 JSON 응답을 테스트 했으므로 file.php -> mod_rewtite -> file.json이 작동합니다. jQuery를 통해. 어쨌든 다음 코드 비트가 첨부되면 JSON 요청이 진실이 아닌 오류 응답을 즉시 트리거합니다. 이유는 무엇입니까?dojo ajax 요청이 json 데이터를 반환하지 않음

sidenotes : 나는 Yandex 주차 미러에서 최신 도조 툴킷 1.7.1을 사용하고 있습니다 : http://yandex.st/dojo/1.7.1/dojo/dojo.js

을 여기 여기

<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> 

또한 작동하지 않습니다 JSON에 대한 코드 것은 file.php입니다 -> file.json 코드 비트도에서 당신이 그것을 필요하지만, 내가 말한대로, 당신은 file.json를 가져 오는하는 URL에 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

완벽하게 작동하기 때문에 나는 그것을 의심하지 않는다 - 그 오류가있는 것 같습니다 정말 file.php해야합니까? 또한

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

경로가 올바른지 확인하십시오 - 당신은

를 보내려고 요청을보기 위해 불을 지르고에서 인터넷 탭을 검사 할 수 있습니다
관련 문제