我想知道是否有任何ES6获取json或其他网址数据的方式。Javascript ES6从URL获取JSON(没有jQuery)
jQuery GET和Ajax调用非常普遍,但我不想在这个中使用jQuery。
一个典型的调用看起来像这样:
var root = 'http://jsonplaceholder.typicode.com';
$.ajax({
url: root + '/posts/1',
method: 'GET'
}).then(function(data) {
console.log(data);
});
或不jQuery的是这样的:
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
else if (xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", "ajax_info.txt", true);
xmlhttp.send();
}
我的问题是...是否有这样做的任何新的方式?例如ES6还是它仍然是一样的方式?
您可能感兴趣的fetch:https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch – Halcyon
Internet Explorer不支持'fetch' –
谁在乎互联网资源管理器? – trincot