我正在嘗試使用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