Python flask data to JS

Source: Internet
Author: User

  1. The first thing to know is the data format used for background and front-end interaction.
    Generally choose JSON, because the perfect fit with JS.

  2. Data returned in the background is serialized
    Returning serialized data in the view method of the/homepagerecommend route

    Dict = {"A": 1, "B": 2}
    1) Import JSON json.dumps (DICT) 2) from Flask import jsonifyjsonify (dict) #在调用jsonfiy sometimes error, because the Jsonify object must be Dict

     The main difference between the two serialization methods is that the jsonify format is more aesthetically pleasing

  3. Using Jquary to deserialize JSON on the front end
    $.getjson ('/homepagerecommend'                , function (data) {                    // returned from flask                      alert (data.a)                     //Browser popup Displays the value of dict["A" returned by the backend, this time is 1
    }
    ) The Getjson function has three parameters //The first is the URL of the data returned bythe back end//The second is to be returned to the server is optional//The third is the deserialized data for the fetch function to proceed with the operation

  4. Then you can use JS to do what you want to do with the data.

Summary: Flask Background to the front-end JS data, need to pay attention to serialization and deserialization

Python flask data to JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.