ajax 取 不了返回值

天道酬勤~ 2011-01-05 11:04:05


function ajaxFunction(url)//////////ajax
{
var xmlHttp;

try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{

// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{

for( var i = 5; i; i-- )
{
try{
if( i == 2 ){
xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP" ); }
else{
xmlhttp_request = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
}
break;
}catch( e)
{
}
}
xmlhttp_request.setRequestHeader("Content-Type","text/xml");
xmlhttp_request.setRequestHeader("Charset","gb2312");
}
}

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
var uuuu= xmlHttp.responseText.toString();
alert(uuuu);///弹出内容,正确。
return uuuu;


}
}
xmlHttp.open("POST",url,true);
xmlHttp.send(null);

}

alert(jaxFunction('http://localhost/index.php'));///弹出undefined

为什么呢?
...全文
86 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
小在在 2011-01-05
  • 打赏
  • 举报
回复
对象:
if(xmlHTTP.readystate==4)
{
......
this.Auuu=uuuu;
}

alert((new ajaxFunction('http://localhost/index.php')).Auuu);
床上等您 2011-01-05
  • 打赏
  • 举报
回复
用jquery方便点吧。
一步一步debug,使用firebug来看看发送和接收的数据
天道酬勤~ 2011-01-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 whg4585 的回复:]

声明一个全局变量:var Auuu="";
if(xmlHTTP.readystate==4)
{
......
Auuu=uuuu;
}
函数结尾处:
xmlHTTP.send(null);

return Auuu==""?"":Auuu;
}
[/Quote]

哦,除了这个方法,没了别的了吗?- -
我还是比较喜欢直接返回呀。
高手还有其它的招没~
小在在 2011-01-05
  • 打赏
  • 举报
回复
声明一个全局变量:var Auuu="";
if(xmlHTTP.readystate==4)
{
......
Auuu=uuuu;
}
函数结尾处:
xmlHTTP.send(null);

return Auuu==""?"":Auuu;
}
天道酬勤~ 2011-01-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 t240034137 的回复:]

alert(jaxFunction('http://localhost/index.php'));///弹出undefined
中“jaxFunction”有没有写错?
[/Quote]

不好意思,我发帖的时候太不小心了,代码里没有打错的。
天道酬勤~ 2011-01-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ihefe 的回复:]

alert(ajaxFunction('http://localhost/index.php'))

少了个a
[/Quote]

哦,真抱歉,哈哈,这也让你看到了。
是我发贴的时候不小心打少的。抱歉。
ihefe 2011-01-05
  • 打赏
  • 举报
回复
alert(ajaxFunction('http://localhost/index.php'))

少了个a
t240034137 2011-01-05
  • 打赏
  • 举报
回复
alert(jaxFunction('http://localhost/index.php'));///弹出undefined
中“jaxFunction”有没有写错?

天道酬勤~ 2011-01-05
  • 打赏
  • 举报
回复
GOOGLE浏览器,IE,都试过 了。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧