• <tfoot id='9yYZW'></tfoot>
      <bdo id='9yYZW'></bdo><ul id='9yYZW'></ul>
  • <legend id='9yYZW'><style id='9yYZW'><dir id='9yYZW'><q id='9yYZW'></q></dir></style></legend>

    1. <i id='9yYZW'><tr id='9yYZW'><dt id='9yYZW'><q id='9yYZW'><span id='9yYZW'><b id='9yYZW'><form id='9yYZW'><ins id='9yYZW'></ins><ul id='9yYZW'></ul><sub id='9yYZW'></sub></form><legend id='9yYZW'></legend><bdo id='9yYZW'><pre id='9yYZW'><center id='9yYZW'></center></pre></bdo></b><th id='9yYZW'></th></span></q></dt></tr></i><div id='9yYZW'><tfoot id='9yYZW'></tfoot><dl id='9yYZW'><fieldset id='9yYZW'></fieldset></dl></div>

      <small id='9yYZW'></small><noframes id='9yYZW'>

        在 javascript 中简单地获取 GET 请求到烧瓶服务器

        时间:2023-10-02

        <legend id='Emt1u'><style id='Emt1u'><dir id='Emt1u'><q id='Emt1u'></q></dir></style></legend>
          • <bdo id='Emt1u'></bdo><ul id='Emt1u'></ul>
              <tbody id='Emt1u'></tbody>

              <tfoot id='Emt1u'></tfoot>

                <small id='Emt1u'></small><noframes id='Emt1u'>

              • <i id='Emt1u'><tr id='Emt1u'><dt id='Emt1u'><q id='Emt1u'><span id='Emt1u'><b id='Emt1u'><form id='Emt1u'><ins id='Emt1u'></ins><ul id='Emt1u'></ul><sub id='Emt1u'></sub></form><legend id='Emt1u'></legend><bdo id='Emt1u'><pre id='Emt1u'><center id='Emt1u'></center></pre></bdo></b><th id='Emt1u'></th></span></q></dt></tr></i><div id='Emt1u'><tfoot id='Emt1u'></tfoot><dl id='Emt1u'><fieldset id='Emt1u'></fieldset></dl></div>
                • 本文介绍了在 javascript 中简单地获取 GET 请求到烧瓶服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将一些 json 数据从烧瓶服务器显示到我的 html 页面,但是在尝试获取资源时出现 TypeError: NetworkError. 带有 Promise { <state>: "被拒绝" }.

                  I'm trying to display some json data from a flask server to my html page but I have a TypeError: NetworkError when attempting to fetch resource. with a Promise { <state>: "rejected" }.

                  server.py

                  from flask import Flask, request, jsonify
                  
                  app = Flask(__name__)
                  
                  
                  @app.route('/hello', methods=['GET'])
                  def hello():
                      jsonResp = {'jack': 4098, 'sape': 4139}
                      print(jsonify(jsonResp))
                      return jsonify(jsonResp)
                  
                  if __name__ == '__main__':
                      app.run(host='localhost', port=8989)
                  

                  script.js

                  function getHello() {
                      const url = 'http://localhost:8989/hello'
                      const response = fetch(url)
                      console.log(response);
                      document.getElementById("demo").innerHTML = response;
                  }
                  
                  

                  index.html

                  <!DOCTYPE html>
                  <html lang="en">
                  <head>
                      <meta charset="UTF-8">
                      <meta name="viewport" content="width=device-width, initial-scale=1.0">
                      <meta http-equiv="X-UA-Compatible" content="ie=edge">
                      <title>Document</title>
                  </head>
                  <body>
                      <button onclick="getHello()">click</button>
                      <label id="demo"></label>
                  
                      <script src="script.js"></script>
                  </body>
                  </html>
                  

                  当我单击按钮时,标签部分中还有一个 [object Promise].

                  I also have a [object Promise] in the label section when I click on the button.

                  我做了最简单的代码,但它不起作用.

                  I did the simplest code possible but it doesn't work.

                  推荐答案

                  这可能是同源策略造成的.浏览器不允许调用不同的来源,除非服务器设置了特殊的 HTTP 标头.如果您从浏览器打开此 html 文件,则服务器的来源 localhost 与浏览器中的来源不匹配,这可能是文件路径.您可以通过将 Access-Control-Allow-Origin 标头添加到响应中来使其工作,如下所示:

                  This could be caused by Same-Origin Policy. Browser does not allow making calls to different origin unless server sets special HTTP header. If you are opening this html file from your browser, the origin of server which is localhost does not match with the origin in your browser which is probably a file path. You can make it work by adding Access-Control-Allow-Origin header to the response as follows:

                  from flask import after_this_request
                  
                  @app.route('/hello', methods=['GET'])
                  def hello():
                      @after_this_request
                      def add_header(response):
                          response.headers['Access-Control-Allow-Origin'] = '*'
                          return response
                  
                      jsonResp = {'jack': 4098, 'sape': 4139}
                      print(jsonify(jsonResp))
                      return jsonify(jsonResp)
                  

                  现在没有网络错误,但你的承诺正在等待,所以你需要为它添加 then.

                  Now there is no network error but your promise is pending so you need to add then for it.

                  或者,您可以通过 Flask 服务器提供 index.html 文件,以便来源匹配.

                  Alternatively you can serve index.html file by your Flask server so that origins match.

                  您可以在此处阅读有关 CORS 和 SOP 的更多信息:

                  You can read more about CORS and SOP here:

                  https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

                  https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

                  这篇关于在 javascript 中简单地获取 GET 请求到烧瓶服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Fetch 返回我自己的 index.html 的 html 源 下一篇:fetch api 无法加载,不支持 url 方案“文件"

                  相关文章

                    <i id='5wlCY'><tr id='5wlCY'><dt id='5wlCY'><q id='5wlCY'><span id='5wlCY'><b id='5wlCY'><form id='5wlCY'><ins id='5wlCY'></ins><ul id='5wlCY'></ul><sub id='5wlCY'></sub></form><legend id='5wlCY'></legend><bdo id='5wlCY'><pre id='5wlCY'><center id='5wlCY'></center></pre></bdo></b><th id='5wlCY'></th></span></q></dt></tr></i><div id='5wlCY'><tfoot id='5wlCY'></tfoot><dl id='5wlCY'><fieldset id='5wlCY'></fieldset></dl></div>
                      <bdo id='5wlCY'></bdo><ul id='5wlCY'></ul>

                  1. <legend id='5wlCY'><style id='5wlCY'><dir id='5wlCY'><q id='5wlCY'></q></dir></style></legend>

                    <small id='5wlCY'></small><noframes id='5wlCY'>

                      <tfoot id='5wlCY'></tfoot>