title: request data from front end (d3.json) from python flask backend
body: <p>Python user, learning some basic javascript:</p>
  
  <p>I have created a simple flask app for demonstration. (For a quick test, I've put this example on Github: <a href="https://github.com/robertdavidwest/flask_d3_example" rel="nofollow noreferrer">https://github.com/robertdavidwest/flask_d3_example</a>)</p>
  
  <pre><code>dependencies:
  - python=3.7.1
  - flask=1.0.2
  - flask-cors=3.0.7
  </code></pre>
  
  <p>and <a href="https://d3js.org/d3.v5.min.js" rel="nofollow noreferrer">https://d3js.org/d3.v5.min.js</a></p>
  
  <p>APP:</p>
  
  <pre><code>.
  ├── run.py
  ├── static
  │   └── js
  │       └── data.js
  └── templates
      └── index.html
  </code></pre>
  
  <p>I'm serving data from a flask app:</p>
  
  <pre><code>from flask import Flask, render_template, jsonify
  from flask_cors import CORS
  
  app = Flask(__name__)
  CORS(app)
  @app.route("/get-data")
  def get_data():
      return jsonify({"a": 1, "b": 2})
  </code></pre>
  
  <p>With the server running, if I navigate to <a href="http://127.0.0.1:5000/get-data" rel="nofollow noreferrer">http://127.0.0.1:5000/get-data</a> then I see the data.</p>
  
  <p>But when I try to use <code>d3.json</code> to request the data from the front end, nothing seems to happen (I've tried logging the data and writing to a div):</p>
  
  <pre><code>d3.json("http://127.0.0.1:5000/get-data",
    function(d){
       console.log(d);
       document.getElementById("d3-write-here").innerHTML = d;
   })
  </code></pre>
  
  <p>Any guidance would be greatly appreciated. </p>
  
  <p>Details of all code in simple app:</p>
  
  <p>index.html:</p>
  
  <pre><code> &lt;!DOCTYPE html&gt;
   &lt;html lang='en'&gt;
   &lt;head&gt;
     &lt;meta charset="utf-8" /&gt;
     &lt;title&gt;Simple Example&lt;/title&gt;
   &lt;/head&gt;
   &lt;body&gt;
    &lt;h2&gt;Nothing much here&lt;/h2&gt;
    &lt;div id="d3-write-here"&gt;&lt;/div&gt;
  
   &lt;script src="https://d3js.org/d3.v5.min.js"&gt;&lt;/script&gt;
   &lt;script src="{{ url_for('static', filename='js/data.js') }}"&gt;
   &lt;/script&gt;
  </code></pre>
  
  <p>
   </p>
  
  <p>run.py:</p>
  
  <pre><code>from flask import Flask, render_template, jsonify
  from flask_cors import CORS
  
  app = Flask(__name__)
  CORS(app)
  
  @app.route("/")
  def index():
      return render_template("index.html")
  
  @app.route("/get-data")
  def get_data():
      return jsonify({"a": 1, "b": 2})
  
  if __name__ == '__main__':
      app.debug = True
      app.run()
  </code></pre>
  
  <p>static/js/data.js:</p>
  
  <pre><code>d3.json("http://127.0.0.1:5000/get-data",
    function(d){
      console.log(d);
      document.getElementById("d3-write-here").innerHTML = d;
    })
  </code></pre>