-3

I'm trying to send data from a server made with Flask in Python, to the client and collect this data with AJAX. Many of the examples I have seen does this with jQuery, but I'd like to do it without jQuery. Is it possible to do this with just ordinary Javascript without jQuery, and how would I build this functionality?

ScandinavianWays
  • 159
  • 2
  • 2
  • 7

2 Answers2

0

You can use the regular XmlhttpRequest: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

Better yet, you can use the Fetch API: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

From the MDN documentation:

 fetch('http://example.com/movies.json')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(myJson);
  });

Fetch makes use of Promise so you should use that.

andrralv
  • 810
  • 7
  • 18
0

You could use the built in XMLHttpRequest object in javascript if you don't want to use jQuery. It's quite simple to use actually,

var url = 'www.yoursite.com/data.json';
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open("GET", url, true);

xhr.onload = function() {
    console.log("Status Code", this.status);
    console.log("Body", this.response);
}
xhr.send();
fixatd
  • 1,394
  • 1
  • 11
  • 19