将全局变量分配给 jQuery post() 访问的 php 数组
我对 jQuery 还很陌生(一周以来),不明白为什么我不能将 PHP (work.php) 数组分配给全局变量 imgArrayThumbs
。我正在调用由 php opendir
函数创建的图像名称数组。当我alert(imgArrayThumbs);
时,我只是返回[object Object]
。有人可以帮忙吗?一整天都在试图弄清楚。
scripts.js
var imgArrayThumbs = '';
$.post("work.php", { task: "imgArrayThumbs" }, function(data) {
imgArrayThumbs = data;
});
alert(imgArrayThumbs);
work.php
include_once 'scripts/functions.php';
if($_POST['task'] == 'imgArrayThumbs'){
imageArray('source/examples/thumbs', 'imgArrayThumbs');
}
elseif ($_POST['task'] == 'imgArray'){
imageArray('source/examples/', 'imgArray');
}
functions.php
function imageArray($dir, $arrayVar){
$arrayName = array();
$iNumber = 0;
$open = opendir ($dir);
while ($file = readdir( $open )){
if($file == "." || $file == ".." || $file == ".DS_Store"){
}else{
$arrayName[$iNumber] = $file;
$iNumber ++;
}
}
closedir ( $open );
for ($i = 0;$i<count($arrayName);$i++){
if ($i == 0) {
echo "\"" . $arrayName[$i] . "\"";
}else{
echo ",\"" . $arrayName[$i] . "\"";
}
}
}
编辑01
工作版本:
scripts.js:< /strong>
var imgArrayThumbs = new Array();
$.ajaxSetup({async:false});
$.ajax({
type: 'POST',
url: 'work.php',
data: { task: "imgArrayThumbs" },
success: function(data) {
imgArrayThumbs = data;
},
dataType: 'json'
});
functions.php:
echo json_encode($arrayName); // without loop
Im pretty new to jQuery (since a week) and don't understand why I can't assign the PHP (work.php) array to global var imgArrayThumbs
. I'm calling an array of image names created by the php opendir
function. When I alert(imgArrayThumbs);
I'm just getting back [object Object]
. Can someone help? Been trying to figure it out all day.
scripts.js
var imgArrayThumbs = '';
$.post("work.php", { task: "imgArrayThumbs" }, function(data) {
imgArrayThumbs = data;
});
alert(imgArrayThumbs);
work.php
include_once 'scripts/functions.php';
if($_POST['task'] == 'imgArrayThumbs'){
imageArray('source/examples/thumbs', 'imgArrayThumbs');
}
elseif ($_POST['task'] == 'imgArray'){
imageArray('source/examples/', 'imgArray');
}
functions.php
function imageArray($dir, $arrayVar){
$arrayName = array();
$iNumber = 0;
$open = opendir ($dir);
while ($file = readdir( $open )){
if($file == "." || $file == ".." || $file == ".DS_Store"){
}else{
$arrayName[$iNumber] = $file;
$iNumber ++;
}
}
closedir ( $open );
for ($i = 0;$i<count($arrayName);$i++){
if ($i == 0) {
echo "\"" . $arrayName[$i] . "\"";
}else{
echo ",\"" . $arrayName[$i] . "\"";
}
}
}
EDIT 01
Working version:
scripts.js:
var imgArrayThumbs = new Array();
$.ajaxSetup({async:false});
$.ajax({
type: 'POST',
url: 'work.php',
data: { task: "imgArrayThumbs" },
success: function(data) {
imgArrayThumbs = data;
},
dataType: 'json'
});
functions.php:
echo json_encode($arrayName); // without loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
alert()
执行toString()
。由于 jQuery 代码返回一个对象,alert
仅打印类类型[object Object]
。相反,使用 Firebug 或 IE 和 Chrome 的内置开发工具,然后执行imgArrayThumbs = data; console.log(imgArrayThumbs)
如果您想浏览对象的内容,请检查控制台。另外,默认情况下 jQuery 需要 JSON 结果,但您的 PHP 代码并未完全将其格式化为正确的 JSON。将其编码为 JSON 的一个更简单的解决方案是删除末尾的
for
循环,并使用echo json_encode($arrayName);
代替。alert()
does atoString()
. Since the jQuery code returns an object,alert
prints the class type only,[object Object]
. Instead, use something like Firebug, or built-in developer tools of IE and Chrome, and doimgArrayThumbs = data; console.log(imgArrayThumbs)
then check the Console if you want to browse the contents of the object.Also, jQuery expects a JSON result by default, but your PHP code isn't exactly formatting it in proper JSON. A much simpler solution of encoding it in JSON, is remove your
for
loop at the end, and useecho json_encode($arrayName);
instead.