1.ajax的使用
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://api.aulence.com/??????', true); //接口地址请慎用
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
// 接受json格式文本字符串
var jsons = JSON.parse(xhr.responseText);
console.log(jsons)
}
}
// 将json文件解析成js对象
var jsObj = JSON.parse(jsons);
2.ajax封装
// 封装Ajax
function getByAjax(type, URL, callback) {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject('Microsoft.XMLHTTP')
};
xhr.open(type, URL, true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
// 接受json格式文本字符串
var jsons = JSON.parse(xhr.responseText);
// console.log(jsons)
callback(jsons)
}
}
}
//调用
getByAjax('GET', 'http://api.aulence.com/????', fn);
function fn(role) {
console.log(role,'元数据');
}