用到了jquery 和 php 的一个ajax方法function imginfo()
{
var strtemp="123";
jq(document).ready(function(){
jq.post(
'dt3.php',
{
a:1
},
function (data)
{
var str=data.split("|");
for(var i=1; i<=str.length-1;i++){
var temparr=str[i-1].split("@");
if(i<str.length-1)
{
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]"+",";
}
else
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]";
}
}
);
});
return strtemp;就上段代码,返回值始终是123 ,不知道什么地方出了问题,我怀疑是变量的作用域不对dt3.php 的代码也贴出来吧,
<?php
require_once './source/class/class_core.php';
require_once './source/function/function_home.php';
$discuz = & discuz_core::instance();
$discuz->init();
//print_r($_G);//$str="1,1,1,1,1,1";
//DB::query("insert into dz_plugin1 (title,content,datetime,username,uid,filename) values ($str)");$result=DB::query("select * from dz_plugin2");$buff2="";
while($rs = mysql_fetch_array($result)){
$buff2.=$rs['title']."@".$rs['url']."@".$rs['filename']."|";
}
echo $buff2;?>
{
var strtemp="123";
jq(document).ready(function(){
jq.post(
'dt3.php',
{
a:1
},
function (data)
{
var str=data.split("|");
for(var i=1; i<=str.length-1;i++){
var temparr=str[i-1].split("@");
if(i<str.length-1)
{
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]"+",";
}
else
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]";
}
}
);
});
return strtemp;就上段代码,返回值始终是123 ,不知道什么地方出了问题,我怀疑是变量的作用域不对dt3.php 的代码也贴出来吧,
<?php
require_once './source/class/class_core.php';
require_once './source/function/function_home.php';
$discuz = & discuz_core::instance();
$discuz->init();
//print_r($_G);//$str="1,1,1,1,1,1";
//DB::query("insert into dz_plugin1 (title,content,datetime,username,uid,filename) values ($str)");$result=DB::query("select * from dz_plugin2");$buff2="";
while($rs = mysql_fetch_array($result)){
$buff2.=$rs['title']."@".$rs['url']."@".$rs['filename']."|";
}
echo $buff2;?>
把它放到ready里面
不好使,提示strtemp 未定义
if(i<str.length-1)
{
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]"+",";
alert( strtemp )
}
这样输来看看是什么//
{
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]"+",";
alert(strtemp);
}
else
{
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]";
alert(strtemp);
}
这样输出的是没问题的,字符串的值是正确的
修改为同步 ;等ajax给strtemp 赋值完毕后,才执行下面的js部分。而异步的话,还没有来得及赋值,就已经return了。然而,如何改为同步呢?谁告诉我一下啊
或者在 回调函数 处理 请求后的逻辑
{
strtemp="123";
jq(document).ready(function(){
//jq.ajaxSetup({ async: false });
jq.ajaxSetup({ async: false, cache: false, global: false });
jq.post(
'dt3.php',
{
a:1
},
function (data)
{
var str=data.split("|");
for(var i=1; i<=str.length-1;i++){
var temparr=str[i-1].split("@");
if(i<str.length-1)
{
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]"+",";
//alert(strtemp);
}
else
{
strtemp += "["+temparr[2]+","+temparr[0]+","+temparr[1]+"]";
alert(strtemp);//跑到这里,输出的值是正确的
}
}
}
);
});
return strtemp;//现在的值就是初始值
}这样也是不对滴