ajax同步得不到数据问题(alert(str)无效)

wangpu666 2017-05-18 12:42:35
1.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax</title>
</head>
<body>
<button onclick="fun()">获取ajax数据</button>
</body>
<script>
function fun(){
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.open('get','1-1.php',false);
alert(xmlhttp.readyState);
xmlhttp.send();
alert(xmlhttp.readyState);
alert(xmlhttp.status);
xmlhttp.onreadystatechange=function(){

if (xmlhttp.readyState==4 && xmlhttp.status==200) {
str=xmlhttp.responseText;
alert(str);
}
}
}
</script>
</html>

1-1.php
<?php echo 'sss'; ?>
...全文
225 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuditao520 2017-06-01
  • 打赏
  • 举报
回复
代码位置放错了 对象.open 和 对象.send(NULL) 放在最后 得数据放在中间
hhl1149 2017-05-31
  • 打赏
  • 举报
回复
php文件不能以数字开头,把php文件名改为英文
wangpu666 2017-05-18
  • 打赏
  • 举报
回复
引用 1楼xuzuning 的回复:
....
xmlhttp.send();
alert(xmlhttp.responseText);
它的原理是为什么
xuzuning 2017-05-18
  • 打赏
  • 举报
回复
....
xmlhttp.send();
alert(xmlhttp.responseText);

21,886

社区成员

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

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