网中网 发表于 2018-12-22 11:13:53

js局部刷新【php技术】

  html代码


[*]
[*]   
[*]         
[*]      js局部刷新
[*]         
[*]            #h1{
[*]                text-align:center;
[*]            }
[*]            #div{
[*]                margin:0 auto;
[*]                width:300px;
[*]                height:200px;
[*]                background:#ccc;   
[*]            }
[*]            button{
[*]                display:block;
[*]                margin:0 auto;
[*]            }
[*]         
[*]   
[*]   
[*]      js局部刷新
[*]         
[*]         
[*]         
[*]      document.write(new Date())
[*]      请求数据get方式
[*]      请求数据post方式
[*]   
[*]   
[*]      //js异步请求php数据(get方式)
[*]      function fasong(){
[*]                var xhr=new XMLHttpRequest();//生成XMLHttpRequest对象
[*]                xhr.onreadystatechange=function(){
[*]                  //当readyState==4 status==200表示响应成功
[*]                if(xhr.readyState==4 && xhr.status==200){
[*]                  //alert(xhr.responseText);
[*]                var data=xhr.responseText;
[*]                var divobj=document.getElementById('div')
[*]                divobj.innerHTML=data;
[*]                //hid.innerHTML=data;
[*]                }
[*]                }
[*]               
[*]                //false 为同步
[*]                xhr.open("get","index.php?",true);//准备好发送数据之前的准备
[*]                xhr.send();//
[*]      }
[*]         
[*]      //js异步请求php数据(post方式)
[*]      function fasong2(){
[*]                var xhr=new XMLHttpRequest();//生成XMLHttpRequest对象
[*]                //alert(xhr);
[*]                /**/
[*]                xhr.onreadystatechange=function(){
[*]                  //当readyState==4 status==200表示响应成功
[*]                if(xhr.readyState==4 && xhr.status==200){
[*]                  //alert(xhr.responseText);
[*]                var data=xhr.responseText;
[*]                var divobj=document.getElementById('div')
[*]                divobj.innerHTML=data;
[*]                //hid.innerHTML=data;
[*]                }
[*]                }
[*]               
[*]                //xhr.open("get","index.php?"+str,true);
[*]                //false 为同步
[*]                xhr.open("post","index.php?",true);//准备好发送数据之前的准备
[*]                xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");//模拟表单 否则无法显示数据
[*]                xhr.send("suername=nihao&age=30");//
[*]      }
[*]         
[*]   
[*]

  php代码


[*]
页: [1]
查看完整版本: js局部刷新【php技术】