简体   繁体   中英

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

I have a route in FLASK that return a jsonify object:

@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:

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

But the out of alert(req) is: [object Object]

to alert(JSON.stringify(req)): {"readyState":1}

to alert(req.html) is: undefined

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('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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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