0

I am sending an XMLHTTPrequest to a flask location like so...

var ourRequest = new XMLHttpRequest();
ourRequest.open("GET", "makeDiff")
diff = ourRequest.send();
console.log(diff);

The request then executes this Python code using Flask...

@app.route('/makeDiff')
def makeDiff():
    diffNew = {"THIS IS A TEST": "HELLO", "TEST2": "HOLA"}
    return json.dumps(diffNew)

However, after the python executes, the variable diff in the javascript is alway undefined. I don't know how to actually send the dictionary back to the javascript. There are many examples on how to do this with JQuery, but is it possible to do this with only javascript and no JQuery.

  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – pmkro Apr 26 '19 at 18:37

1 Answers1

0
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        alert(xhr.responseText);
    }
}
xhr.open('GET', 'http://example.com/makeDiff', true);
xhr.send(null);

onreadystatechange callback is called when response arrive from python.

Wang Liang
  • 4,244
  • 6
  • 22
  • 45