【问题标题】:Why my JSON script dont GET a JSONIFY return from FLASK?为什么我的 JSON 脚本没有从 FLASK 获得 JSONIFY 返回?
【发布时间】:2019-05-16 22:10:22
【问题描述】:

我在 FLASK 中有一条返回 jsonify 对象的路由:

@app.route('/getgoodbye', methods=['GET', 'POST'])
def getgoodbye():
    return jsonify({'html':'<h1>Good Bye</h1>'})

以及需要获取此对象的 jscript 中的函数:

    function getgoodbye2(){
        var req = $.getJSON('/getgoodbye');
        alert(req);
    }

但是alert(req)的输出是:[object Object]

提醒(JSON.stringify(req)): {"readyState":1}

to alert(req.html) 是:未定义

如何访问字典的键和值?

【问题讨论】:

    标签: python html json flask jscript


    【解决方案1】:

    由于 $.getJSON 异步工作,您需要在回调中进行,我在 myjson.com (https://api.myjson.com/bins/9ug2k) 中创建了 JSON,这里是代码 sn-p

    $.getJSON('https://api.myjson.com/bins/9ug2k',function(resp){
       console.log(resp.html)
    })
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

    希望对你有帮助!

    【讨论】:

    • 哦!我得到一些东西。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2013-05-09
    • 2016-04-06
    • 2019-07-09
    • 2018-07-06
    • 2016-10-22
    • 2014-08-07
    相关资源
    最近更新 更多