Wednesday, June 5, 2024
141
rated 0 times [  144] [ 3]  / answers: 1 / hits: 21183  / 7 Years ago, thu, june 8, 2017, 12:00:00

I was wondering if there's any ES6 way of getting json or other data from a url.



jQuery GET and Ajax calls are very common but I don't want to use jQuery in this one.



A typical call would look like this:



var root = 'http://jsonplaceholder.typicode.com';

$.ajax({
url: root + '/posts/1',
method: 'GET'
}).then(function(data) {
console.log(data);
});


or without jQuery something like this:



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();
}


My question is...Is there any new ways of doing this ... for example ES6 or is it still the same way?


More From » ecmascript-6

 Answers
5

FETCH API



Example:



// url (required), options (optional)
fetch('https://davidwalsh.name/some/url', {
method: 'get'
}).then(function(response) {

}).catch(function(err) {
// Error :(
});


For more information:



https://developer.mozilla.org/en/docs/Web/API/Fetch_API


[#57529] Tuesday, June 6, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocky

Total Points: 316
Total Questions: 108
Total Answers: 110

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;