简体   繁体   English

为什么我的JSON脚本无法从FLASK获取JSONIFY返回?

[英]Why my JSON script dont GET a JSONIFY return from FLASK?

I have a route in FLASK that return a jsonify object: 我在FLASK中有一条返回jsonify对象的路由:

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

and a function in jscript that need get this object: 和jscript中需要获取此对象的函数:

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

But the out of alert(req) is: [object Object] 但是超出警报范围的是:[对象对象]

to alert(JSON.stringify(req)): {"readyState":1} 警报(JSON.stringify(req)):{“ readyState”:1}

to alert(req.html) is: undefined 警报(req.html)是:未定义

How do I can acess the key and values of the dict? 如何获取字典的键和值?

Since $.getJSON works async you need to do it inside callback, I have created the JSON in myjson.com ( https://api.myjson.com/bins/9ug2k ) and here is the code snippet 由于$ .getJSON可以异步工作,因此您需要在回调内部进行操作,因此我在myjson.com( https://api.myjson.com/bins/9ug2k )中创建了JSON,这是代码段

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

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM