【问题标题】:Ajax call return list from python flask approute来自 python 烧瓶应用程序路由的 Ajax 调用返回列表
【发布时间】:2013-03-29 18:26:03
【问题描述】:

我正在使用 ajax 调用来发布一些数据,现在我需要将一个 python 列表从 approute 返回到 html。我怎么能做到这一点。这是我的代码

                       $('#stop').submit(function(){
 id = id
 var services = $('#service').val()
 var extra  = document.getElementById('More').value
 $.ajax({
     type: 'post',
     url: "/stopservice/"+id,
     cache:false,
     async:true,
     data: {service: JSON.stringify(services),
            extra: JSON.stringify(extra),
     },
     success: function(data) {
         alert(data)

     },
     error: function(data)
     {               $.unblockUI();
         alert(data)
     }
 });
 return false
      });
       });

这是我的approute。 listofdata = 决赛名单 返回数据列表

所以基本上 listof data 是一个 python 列表..这是 approute 的返回值。但我无法在 ajax 中获取这些数据。任何帮助

【问题讨论】:

  • 您有一个拼写错误:必须删除最后一个});。

标签: javascript jquery python ajax flask


【解决方案1】:

您需要使用 Flask 的 jsonify 函数(或使用 json.dumps 自行将列表转换为 JSON 并将其作为 application/json 发送回)。

# In your route handler
return jsonify(list_of_data=list_of_data)

 

// In your JavaScript
success: function(data) {
    console.log(data.list_of_data);
}

【讨论】:

  • 有没有其他方法,因为我使用了for循环将列表数据绑定到html表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
  • 2018-01-22
  • 1970-01-01
  • 2020-11-16
相关资源
最近更新 更多