【问题标题】:getJson doesnt work when calling python script调用python脚本时getJson不起作用
【发布时间】:2012-11-15 23:19:29
【问题描述】:

我有以下简单的代码,但 getjson 不知何故不起作用。 我的 python 脚本正确打印了一个 json 编码的值。 任何人都可以提出任何建议吗?

HTML

<!DOCTYPE html>
<html>
 <head>
   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
 </head>
 <body>
   <script type="text/javascript">

   $.getJSON('weather.py', function(data) {
    console.log('callback works');
  });


   </script>
 </body>
</html>

Python:

import json
from json import load
from urllib2 import urlopen
from pprint import pprint
import pycassa
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
from pycassa.index import *

pool = pycassa.ConnectionPool('hr')
crime = pycassa.ColumnFamily(pool, 'crime')

##Pull all of the data and convert to JSON 
data = json.dumps([columns for key, columns in crime.get_range()])
return data

【问题讨论】:

    标签: javascript python html getjson


    【解决方案1】:

    您不能从 Jquery 运行 python 脚本。

    您需要让您的 python 脚本与网络服务器一起运行以提供响应。

    起点:http://docs.python.org/2/howto/webservers.html

    【讨论】:

      【解决方案2】:

      我假设您正确缩进了您的 python 脚本。我还假设您已经配置了网络服务器环境。除此之外

      代替

      return data
      

      你必须

      print data
      

      在您的 python 脚本中。因为$.getJSON 将传递您脚本的输出,而不是您返回的变量的内容。

      【讨论】:

        猜你喜欢
        • 2015-01-16
        • 2017-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-12
        • 2011-06-12
        • 2010-12-16
        相关资源
        最近更新 更多