ajax同步得不到数据问题(alert(str)无效)
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'; ?>