2010-11-09 53 views
0

我正在嘗試使用JSONP將數組從PHP返回到JavaScript。我希望,我的代碼將證明我正在嘗試做什麼,因爲我甚至不知道如何將它字...使用JSONP將數組從PHP返回到JavaScript

我的PHP文件,端口80,因此需要使用JSONP而不是JSON(I試過的話) 我不知道如果我形成$ _GET變量正確要麼,我敢肯定這是錯的,我的知識的缺乏是這樣做的原因...

<?php 
$directory = './thumbnails/'; 

// create a handler to the directory 
$dirhandler = opendir($directory); 

// read all the files from directory 
$nofiles=0; 
while (false !== ($file = readdir($dirhandler))) { 

// if $file isn't this directory or its parent 
//add to the $files array 
     if ($file != '.' && $file != '..') 
     { 
      $thumbs[$nofiles]= 'http://localhost:80/mapScripts/thumbnails/' . $file; 
      $nofiles++;    
     } 
} 

//$i = rand(0, 3); 


//$output = "{"; 
for($i=0; $i < 3; $i++){ 
$json[i] = json_encode($thumbs[$i]); 
$output = $output . $_GET['thumbnails' . $i]. "(".$json[i].")"; 
//$output = $output . "'thumb" . $i . "':'" . $thumbs[$i] . "',"; 
} 

//$output = $output . "}"; 
//echo $_GET['thumbnails'] ."(".$json.")"; 

echo $output; 

?> 

然後在JavaScript在端口8080(跨域,是的,它工作正常,直到我試圖使用這個數組,而不是隻傳遞一個圖像url)我想從PHP數組中獲取每個圖像url,以便我可以使用圖像製作圖標..

function makeThumbs(data, layer){ 
       var icon = new OpenLayers.Icon(data); 
       layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(93.9, 29.53).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")),icon)); 
       for(var m = 0; m < layer.markers.length; m++){ 
        layer.markers[m].events.register("click", layer.markers[m], function clickIcon(e){alert("How are you?");}); 
        $("[id$=innerImage]").css({"border-style":"solid","border-width":"3px","border-color": "white"}); 
       } 
      } 

      $.getJSON('http://localhost:80/mapScripts/getThumbs.php?thumbnails2=?', function(data) {makeThumbs(data, markers);}); 

再次傳遞給$ .getJSON方法的url也可能是錯誤的。我需要知道如何從傳遞的數組中選擇確切的照片url,而不是所有的JSONP數據。

我很感激您的時間和反饋幫助我。

elshae

回答

0

我居然發現在做it..Here去的一種方式..

<?php 
$directory = './thumbnails/'; 

// create a handler to the directory 
$dirhandler = opendir($directory); 

// read all the files from directory 
$nofiles=0; 
while (false !== ($file = readdir($dirhandler))) { 

// if $file isn't this directory or its parent 
//add to the $files array 
     if ($file != '.' && $file != '..') 
     { 
      $thumbs[$nofiles]= 'http://localhost:80/mapScripts/thumbnails/' . $file; 
      $nofiles++;    
     } 
} 

//$i = rand(0, 3); 


$output = $_GET['thumbnails'] . "({"; 
for($i=0; $i < 3; $i++){ 
//$json[i] = json_encode($thumbs[$i]); 
//$output = $output . $_GET['thumbnails' . $i]. "(".$json[i].")"; 
$output = $output . "'thumb" . $i . "':'" . $thumbs[$i] . "',"; 
} 

$output = $output . "})"; 
//echo $_GET['thumbnails'] ."(".$json.")"; 

echo $output; 

?> 

輸出: ({ 'thumb0':'HTTP://本地主機:80/mapScripts /thumbnails/Tibet2.jpeg','thumb1':'http://localhost:80/mapScripts/thumbnails/lhasa.jpeg','thumb2':'http://localhost:80/mapScripts/thumbnails/Tibet.jpg ',})

然後在JavaScript中我只是:

function makeThumbs(data, layer){ 
       alert("already Here "+ data); 
       var icon = new OpenLayers.Icon(data); 
       alert(icon.url); 
       layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(93.9, 29.53).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")),icon)); 
       for(var m = 0; m < layer.markers.length; m++){ 
        layer.markers[m].events.register("click", layer.markers[m], function clickIcon(e){alert("How are you?");}); 
        $("[id$=innerImage]").css({"border-style":"solid","border-width":"3px","border-color": "white"}); 
       } 
      } 

      $.getJSON('http://localhost:80/mapScripts/getThumbs.php?thumbnails=?', function(data) {makeThumbs(data.thumb2, markers);}); 

因此,似乎在$ _GET變量之後,您可以放置​​典型的JSON數據並像往常一樣抓取它(注意JavaScript中的data.thumb2)。