2

How to use $.getJSON in JavaScript to retrieve a python file data?

$.getJSON('data.py', function (keyData) {
//code
})

data.py

import urllib.request, json 
def jsonURL():
    with urllib.request.urlopen("https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/usdeur.json") as url:
    data = json.loads(url.read().decode())
    return data
2
  • You can’t use getJSON to query the Python file - that needs to be $.get Commented Dec 2, 2018 at 2:49
  • Still not getting the JSON data from the python file to show up. Commented Dec 2, 2018 at 2:57

1 Answer 1

2

Use $.ajax instead to get the Python file:

$.ajax({
    type: "get",
    url: "data.py",
    success: function(response) {
        //Do stuff
    }
});

This will query the Python file. However, as shown here, you need to tell your web server to execute the file instead of returning the contents.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.