首页 > 解决方案 > JS从flask中获取返回0数组长度

问题描述

我有一个非常简单明了的代码:

派:

@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; 
})

然而我得到:

在此处输入图像描述

我不确定这是为什么。

标签: javascriptpythonjsonpostfetch

解决方案


我不确定您的 Python 代码,但fetch在 js 中,您需要json先将响应转换为。

这应该有效:

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

推荐阅读