title: How do I return JSON-data from python to javascript?
body: <p>I'm working on a pythonfile which returns the variable "distance" and sends it to a javascriptfile where I can put the value on my webpage. </p>
  
  <p>My problem is that I don't know how to send the value from python to javascript. I've heard you have to make the pythonfile return in JSON-format and then make a ajax-request, but I can't find how to do it anywhere.</p>
  
  <p>My question is: How do I set up the connection which makes me get the JSON-data in javascript? I would really appriciate if someone showed me using code, I'm very new to both python and javascript..</p>
  
  <p>Edit: 
  The data comes from a RaspberryPi with a distance-sensor. My python code is: </p>
  
  <pre><code>import RPi.GPIO as GPIO
  import time
  import io, json
  
  GPIO.setmode(GPIO.BCM)
  TRIG = 14
  ECHO = 15
  
  GPIO.setup(TRIG,GPIO.OUT)
  GPIO.setup(ECHO,GPIO.IN)
  
  revers = 1
  
  while revers == 1:
  
      GPIO.output(TRIG,0)
      time.sleep(0.5)
  
      GPIO.output(TRIG,1)
      time.sleep(0.00001)
      GPIO.output(TRIG,0)
  
      while GPIO.input(ECHO)==0:
          pulse_start = time.time()
  
      while GPIO.input(ECHO)==1:
          pulse_end = time.time()
  
      pulse_duration = pulse_end - pulse_start
      distance = pulse_duration * 17150
  GPIO.cleanup()
  </code></pre>
  
  <p>I'm not yet to write anything in javascript, simply because I don't know how to make the ajax call, but my goal is to make the variable distance into JSON-format and get it up on my webpage. </p>