-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
  • 44,308
  • 16
  • 74
  • 100
John
  • 35
  • 7

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
  • 811
  • 1
  • 9
  • 26
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
  • 116,827
  • 30
  • 260
  • 317