原生ajax动态添加数据

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>

    function ajax(method, url, date,success){
        
        var xhr = null;
        try{
            xhr = new XMLHttpRequest(); 
        }catch(e){
            xhr = new ActiveXObject('Microsoft.XMLHTTP');
        }        
        if(method == 'get' && date){
            url += '?'+date;
        }
        xhr.open(method,url,true);
        
        if(method == 'get'){
            xhr.send();
        }else{
            xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
            xhr.send(date);
        }
        
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(xhr.status == 200){
                    success && success(xhr.responseText);
                }else{
                        alert('出错了'+xhr.status);
                }
            }
        }
    }

    
    window.onload = function(){
        var btn = document.getElementById('btn');
        btn.onclick = function(){
            ajax('get', 'getNews.php', '',function(date){
                    var date = JSON.parse(date);
                    var oUl = document.getElementById('list');
                    var value = '';
                    for(var i=0;i<date.length;i++){
                        value += '<li><a href="#">'+date[i].title+'</a>&nbsp;<span>'+date[i].date+'</span></li>'
                    }
                    oUl.innerHTML = value;
            });
            var timer = null;
            clearInterval(timer);
            timer = setInterval(function(){
                ajax('get','getNews.php','',function(date){
                    var date = JSON.parse(date);
                    var oUl = document.getElementById('list');
                    var value = '';
                    for(var i=0;i<date.length;i++){
                        value += '<li><a href="#">'+date[i].title+'</a>&nbsp;<span>'+date[i].date+'</span></li>'
                    }
                    oUl.innerHTML = value;
                });
            },3000);
        };
        
    };
</script>
</head>

<body>
    <input type="button" value="按钮" id="btn"/>
    <ul id="list"></ul>
</body>
</html>
<?php
    header('content-type:text/html; charset="utf-8"');
    error_reporting(0);
    
    $news = array(
            array('title'=>'12121212121牌','date'=>'2018-6-8'),
            array('title'=>'美又打2000亿关税牌','date'=>'2018-6-1'),
            array('title'=>'北京平均可月省约150','date'=>'2018-6-2'),
            array('title'=>'个税起征点提至5000','date'=>'2018-6-3'),
            array('title'=>'美又打2000亿关税','date'=>'2018-6-4'),
            array('title'=>'哥伦比亚vs日本:J罗','date'=>'2018-6-5'),
            array('title'=>'北京时间"一粽浓情"活动结束','date'=>'2018-6-6'),
        );
    echo json_encode($news);

 

posted on 2018-06-21 20:09  朝颜陌  阅读(481)  评论(0编辑  收藏  举报

导航