【问题标题】:How to use $.getJSON in JavaScript to retrieve a python file data?如何在 JavaScript 中使用 $.getJSON 来检索 python 文件数据?
【发布时间】:2019-05-03 17:58:22
【问题描述】:

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

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

数据.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

【问题讨论】:

  • 你不能使用 getJSON 来查询 Python 文件 - 这需要是 $.get
  • 仍然没有从 python 文件中获取 JSON 数据来显示。

标签: javascript python jquery json get


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多