你的位置:首页 > Java教程

[Java教程]ajax.js


function ajax(method, url, data, success) {  //success是一个回调函数	var xhr = null;	try {		xhr = new 
oBtn.onclick = function() {				ajax('get','getNews.php','',function(data) {			var data = JSON.parse( data ); //转换							var oUl = document.getElementById('ul1');			var html = '';			for (var i=0; i<data.length; i++) {				html += '<li><a href="">'+data[i].title+'</a> [<span>'+data[i].date+'</span>]</li>';			}			oUl.innerHTML = html;		});				setInterval(function() {  //定时刷新获取数据			ajax('get','getNews.php','',function(data) {				var data = JSON.parse( data );									var oUl = document.getElementById('ul1');				var html = '';				for (var i=0; i<data.length; i++) {					html += '<li><a href="">'+data[i].title+'</a> [<span>'+data[i].date+'</span>]</li>';				}				oUl.innerHTML = html;			});		}, 1000);	}