如何在JavaScript中使用$ .getJSON检索python文件数据?

时间:2018-12-02 02:38:18

标签: javascript python jquery json get

如何在JavaScript中使用$ .getJSON检索python文件数据?

$.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

1 个答案:

答案 0 :(得分:2)

使用$.ajax来获取Python文件:

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

这将查询Python文件。但是,如here所示,您需要告诉Web服务器执行文件而不是返回内容。