user13888987
user13888987

Reputation:

JS fetch returning 0 array length from flask

I have a very simple and straightforward code:

Py:

@blueprint.route('/Ajax', methods=['GET', 'POST']) 
def Ajax():                
    Graph1 = [10,10,10,10,10]
    return jsonify(Graph1)

JS

fetch('/Ajax')
.then(function (response) {
  theData = Object.values(response); 
  console.log(theData);
  return theData; 
})

Yet I am getting:

enter image description here

I am not sure why this is the reason.

Upvotes: 0

Views: 327

Answers (1)

Dominik
Dominik

Reputation: 6313

I'm unsure about your Python code but with fetch in js you need to convert the response to json first.

This should work:

fetch('/Ajax')
  .then(response => response.json())  // <--- this has been added
  .then(function (response) {
    theData = Object.values(response); 
    console.log(theData);
    return theData; 
  });

Upvotes: 1

Related Questions