1. 程式人生 > >原生ajax動態添加數據

原生ajax動態添加數據

ID err 原生 北京 xmlns inter urlencode IT pub

<!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);

原生ajax動態添加數據