-6

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);
elixenide
  • 43,445
  • 14
  • 72
  • 97
John
  • 35
  • 6

2 Answers2

1

Using 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.

Alvaro Castro
  • 771
  • 1
  • 7
  • 25
0

You can use fetch API for Ajax request.

fetch("csv_data.csv", { headers: { "Content-Type": "text/csv" } })
  .then(function(response) {
    return response.txt();
  })
  .then(successFunction);
document.body.appendChild(table);
Arup Rakshit
  • 113,563
  • 27
  • 250
  • 306