function ajax_(methods, url, callback) {
var ajax_ = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
var result;
ajax_.open(methods, url);
ajax_.send();
ajax_.onreadystatechange = function () {
if (ajax_.readyState == 4) {
if (ajax_.status == 200) {
result = ajax_.responseText;
result = JSON.parse(result);
callback(result);
} else {
console.log('请求失败');
}
}
}
}
使用方法
ajax_('get', 'http://www.chenfuguo.cn:5600/news', function (result) {
if (result.status == 200) {
let news = result.news.data;
let content = document.getElementsByClassName('newss')[0];
for(var data of news){
var item = document.createElement('div');
content.innerHTML += `
<div class="newsFor">
<div class="newsSingle">
<img src="${data.picUrl}" alt="">
<div class="newsContent">
<div class="contentFont">
<p>${data.title}</p>
<span>${data.content}</span>
</div>
<div class="contentImg">
<p class="times">${data.time}</p>
<img src="./img/you.png" alt="">
</div>
</div>
</div>
</div>`
}
}
});