How do I convert jQuery AJAX code to vanilla JavaScript? [duplicate]

Clash Royale CLAN TAG#URR8PPPHow do I convert jQuery AJAX code to vanilla JavaScript? [duplicate]
This question already has an answer here:
I have an AJAX script in jQuery that I am trying to convert to vanilla Javascript. I Can't seem to convert this. How would I convert this to vanilla JavaScript?
$.ajax({
url: 'csv_data.csv',
dataType: 'text',
}).done(successFunction);
$('body').append(table);
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
fetch(url, request).then(function(respose) { successFunction(response)})
– Egor Egorov
1 hour ago
You can check the source code of jquery on github
– Alon Eitan
1 hour ago
How to make an AJAX call without jQuery?
– Jonathan Lonowski
1 hour ago
2 Answers
2
Using XMLHttpRequest
XMLHttpRequest
var r = new XMLHttpRequest();
r.open('GET', 'csv_data.csv');
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
successFunction(r.responseText);
};
r.send();
Haven't tested it.
You can use fetch API for Ajax request.
fetch
fetch("csv_data.csv", { headers: { "Content-Type": "text/csv" } })
.then(function(response) {
return response.txt();
})
.then(successFunction);
document.body.appendChild(table);
That is plain javascript. Do you mean you don't want to use jQuery?
– Mark Meyer
1 hour ago