Javascript 需要从AJAX调用(flask后端)加载动态生成的字符串列表

Javascript 需要从AJAX调用(flask后端)加载动态生成的字符串列表,javascript,python,ajax,list,flask,Javascript,Python,Ajax,List,Flask,我认为标题说明了一切,但基本上,这是我的flask/python后端 @app.route('/failing_jobs/', methods= ['GET' , 'POST']) def failing_jobs(): real_list = generate_failing_jobs() #normal method that will generate a list dummy_list = ['jobA', 'jobB', 'jobC'] return dumm

我认为标题说明了一切,但基本上,这是我的flask/python后端

@app.route('/failing_jobs/', methods= ['GET' , 'POST'])
def failing_jobs():

    real_list = generate_failing_jobs() #normal method that will generate a list
    dummy_list = ['jobA', 'jobB', 'jobC']
    return dummy_list
现在在我的模板中,我有一个脚本

<!-- necesary j-Query includes above this point-->
<script type= "text/javascript">
    $(document).ready(function(){
        alert("Before the ajax call");
        $.ajax({
            url : "/failing_jobs/",
            type : "GET",
            success : function(data){
                alert(data);
            }
        });
        alert("After the ajax call");
    });
</script>

有人知道我可以输出一个列表的方法吗,或者我需要把它分开,或者什么?如何通过ajax加载动态生成的列表?

必须返回字符串、元组(包含带有可选状态代码和标题的字符串)或响应。列表不是此处的选项之一。改用
jsonify()
(命名列表):

在jQuery端使用:

$.ajax({
    url : "/failing_jobs/",
    type : "GET",
    contentType: "application/json",
    success : function(data){
        alert(data.jobs);
    }
});

错误不属于您显示给我们的代码的这一部分。如果后端出现故障,则不会发送HTTP 20x(主要是50x),也不会调用successhandler。建议:使用$.getJSON()作为检索JSONformatted数据的快捷方式。@ThomasJunk,是的。我在调用“return dummy_list”时得到它,因此,如果用
['jobA','jobB','jobC']
替换
dummy_list
,并删除
real_list=generate_failing_jobs()
结果应该就是您的错误消息,对吗?正确。我加入了“真实清单”作为我正在做什么的背景。“我知道我正确地生成了列表,因为一条打印语句告诉我是这样的。@ThomasJunk,我使用了flask.jsonify,我似乎越来越接近了。然而,json结构使得列表中的所有内容都分配给一个键。Martjin,毫无疑问,你是我在这个网站上遇到的最好的flask程序员(我想我缺少了“contentType”)。实际上,我对显示信息有疑问。如何将列表中的每个作业显示为页面上的链接?比如,如果我只有一根绳子,那就容易了$(“#作业失败”).html()。。。您知道如何为多个作业动态执行此操作吗?我会继续找,但我想你会知道怎么做的;jQuery有很棒的工具来循环列表并从中创建新的标记;一个快速的谷歌“JSON链接列表”会给你大量的堆栈溢出答案;jQuery并不是我的强项,所以我只想让您参考一下。
from flask import jsonify

@app.route('/failing_jobs/', methods= ['GET' , 'POST'])
def failing_jobs():
    real_list = generate_failing_jobs() #normal method that will generate a list
    return jsonify(jobs=real_list)
$.ajax({
    url : "/failing_jobs/",
    type : "GET",
    contentType: "application/json",
    success : function(data){
        alert(data.jobs);
    }
});