Python Flask get json data to display

前端 未结 1 1119
庸人自扰
庸人自扰 2021-01-02 10:35

I\'m currently trying to display a list of values that get updated every 5 seconds to a sqlite database.

I can manage to convert the results to a JSON format by usin

1条回答
  •  迷失自我
    2021-01-02 11:19

    Your AJAX call should auto-detect a JSON response, but it won't hurt to explicitly tell jQuery about it:

    $.ajax({
        type: "GET",
        url: $SCRIPT_ROOT + "_status",
        dataType: 'json',
        success: function(data) {
            $('#Result').text(data);
        }
    );
    

    The contentType parameter is only used for a POST request, telling the server what type of data you sent.

    The data object contains whatever your Flask jsonify() response returned; in this case it'll be a JavaScript Object with the BoilerRoom, etc. keys.

    Since you are loading JSON over a GET request, you may as well use the jQuery.getJSON() method here:

    $.getJSON(
        $SCRIPT_ROOT + "_status",
        function(data) {
            $('#Result').text(data);
        }
    );
    

    This does exactly the same as the $.ajax() call but you get to omit the type and dataType parameters, and the url and success parameters are just positional elements.

    0 讨论(0)
提交回复
热议问题