<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                https://segmentfault.com/a/1190000007605055 最近在學習Python的flask微框架,用到最多的就是和前端針對REST API進行交互,那么自然而然傳遞Json就成為了家常便飯,特意總結了一下和前端交互幾種方法。 ### 1\. 利用flask的**request.form.get()**方法 這一中方法主要利用flask的**request.form.get**方法,獲得前端發送給后臺的json文件 Python 端代碼: ~~~ @app.route('/sendjson', methods=['POST']) def sendjson(): # 接受前端發來的數據 data = json.loads(request.form.get('data')) # lesson: "Operation System" # score: 100 lesson = data["lesson"] score = data["score"] # 自己在本地組裝成Json格式,用到了flask的jsonify方法 info = dict() info['name'] = "pengshuang" info['lesson'] = lesson info['score'] = score return jsonify(info) ~~~ JS代碼: ~~~ <script> $(document).ready(function () { var data = { data: JSON.stringify({"lesson": "Operation System", "score": 100}) } $.ajax({ url:"/sendjson", type: 'POST', data: data, success: function (msg) { alert(msg.name) } }) }); </script> ~~~ ### 2\. 利用flask的**request.get_data()**方法 Python端代碼: ~~~ @app.route('/sendjson2',methods=['POST']) def sendjson2(): # 接收前端發來的數據,轉化為Json格式,我個人理解就是Python里面的字典格式 data = json.loads(request.get_data()) # 然后在本地對數據進行處理,再返回給前端 name = data["name"] age = data["age"] location = data["location"] data["time"] = "2016" # Output: {u'age': 23, u'name': u'Peng Shuang', u'location': u'China'} # print data return jsonify(data) ~~~ 相比第一種方法JS的代碼,這一種更常見也更容易理解一些,在前端組織好Json,再傳遞給后臺 JS代碼: $(document).ready(function () { var student = new Object(); student.name = &quot;Peng Shuang&quot;; student.age = 23; student.location = &quot;China&quot;; var data = JSON.stringify(student) $.ajax({ url: &quot;/sendjson2&quot;, type: &quot;POST&quot;, data: data, success: function (msg) { alert(msg.time) } }) }) " title="" data-original-title="復制" style="box-sizing: border-box; cursor: pointer; display: block; float: left; background-image: url(&quot;../img/codeTools.svg&quot;); height: 16px; width: 16px; background-size: auto 16px; margin: 4px 5px; opacity: 0.5; background-position: -16px 0px !important;"> ~~~ <script> $(document).ready(function () { var student = new Object(); student.name = "Peng Shuang"; student.age = 23; student.location = "China"; var data = JSON.stringify(student) $.ajax({ url: "/sendjson2", type: "POST", data: data, success: function (msg) { alert(msg.time) } }) }) </script> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看