0715JS基础:ajax

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
</head>
<body>
    <div id="nr">
        
    </div>
</body>
</html>
<script type="text/javascript">
    //1、初始化xml http request对象
    if(window.XMLHttpRequest){
        var xml = new XMLHttpRequest();
    }else{
        var xml = new ActiveXObject('Microso.XMLHTTP');
        //针对IE5/IE6浏览器
    }
    var attr = [1,2,3];
    //2、发送请求
    //get方式发送
    //xml.open("GET","chuli.php?fname="+attr,true);
    //传递的变量写到url地址
    //xml.send();
    //post方式发送
    xml.open("POST","chuli.php",true);
    xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xml.send("fname="+attr);
    //3、接受返回值
    xml.onreadystatechange=function(){
        if(xml.readyState==4 && xml.status==200){
            var str=xml.responseText;
            //以字符串形式传回,所以处理页面也要把数据整合成字符串
            console.log(str);
        }
        document.getElementById("nr").innerHTML=str;
    }
</script>

 

posted @ 2018-07-15 22:23  359950470  阅读(79)  评论(0编辑  收藏  举报