加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax数据交互--GET

发布时间:2020-12-16 02:48:12 所属栏目:百科 来源:网络整理
导读:GET方法实现从服务器获取数据 HTML代码 ! DOCTYPE html html lang ="en" 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 / tit

GET方法实现从服务器获取数据

HTML代码

<!DOCTYPE html>
<html lang="en">
<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>
    <script>
        function funget(){
            var xhr;
            if(window.XMLHttpRequest){
                xhr = new XMLHttpRequest;
            }else{
                xhr = new ActiveXObject(Microsoft.XMLHTTP);
            }
            xhr.onreadystatechange = function (){
                if(xhr.readyState == 4 && xhr.status == 200){
                    document.getElementById(content).innerHTML = xhr.responseText;
                }
            }
            xhr.open(GET,get.php,true);
            xhr.send();
        }
    
    </script>
    <style>
        .root{
            text-align: center
        }
        #content{
            color: red;
            font-size: 30px;
        }
    </style>
</head>
<body>
    <div class="root">
       <button onclick="funget()">点一下</button>
       <br><br><br>
        <div id="content"></div> 
    </div>
    
</body>
</html>

页面是这样:

点击按钮 获取数据? PHP代码部分

<?php
    echo ‘GET方法<br>‘;
    echo date(‘Y-m-d H:i:s‘,time());
?>

这里是点击获取的时间,点击按钮后页面不会刷新,实现了局部的数据交互,显示为:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读