使用 JSONP 将数组从 PHP 返回到 JavaScript

发布于 2024-10-01 14:24:46 字数 2055 浏览 0 评论 0原文

我正在尝试使用 JSONP 将数组从 PHP 返回到 JavaScript。希望我的代码能够准确地演示我想要做什么,因为我什至不太确定如何表达它...

我的 PHP 文件,端口 80,因此需要使用 JSONP 而不是 JSON(我已经尝试过) 我也不确定我是否正确地形成了 $_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;

?>

然后在端口 8080 上的 JavaScript 中(跨域,是的,它有效很好,直到我尝试使用这个数组而不是只传递一个图像 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 数据。

感谢您的宝贵时间和反馈帮助我解决此问题。

埃尔谢

I am attempting to use JSONP to return an array from PHP to JavaScript. Hopefully, my code will demonstrate exactly what I'm trying to do because I am not even so sure how to word it...

My PHP file, port 80, hence the need to use JSONP and not JSON (I tried already)
I am not sure if I am forming the $_GET variables correctly either, I'm pretty certain it's wrong though and my lack of knowledge is the reason for this...

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

?>

Then in JavaScript on port 8080 (cross-domain and yes it worked fine until I tried to use this array as opposed to just passing one image url) I want to get each image url from the PHP array so that I can make icons using the image..

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);});

again the url I am passing to the $.getJSON method is also probably wrong. I need to know how to select the exact photo url from the array being passed, not all the JSONP data.

I appreciate your time and feedback for helping me with this.

elshae

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2024-10-08 14:24:47

我实际上找到了一种方法..这里是..

<?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://localhost: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)。

I actually found one way of doing it..Here goes..

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

?>

That outputs:
({'thumb0':'http://localhost:80/mapScripts/thumbnails/Tibet2.jpeg','thumb1':'http://localhost:80/mapScripts/thumbnails/lhasa.jpeg','thumb2':'http://localhost:80/mapScripts/thumbnails/Tibet.jpg',})

Then in JavaScript I just:

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);});

So it seems after the $_GET variable you can put typical JSON data and fetch it as you normally would (notice the data.thumb2 in the JavaScript).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文