1

次の簡単なコードがありますが、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>

パイソン:

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
4

2 に答える 2

2

Jquery から Python スクリプトを実行することはできません。

応答を提供するには、ウェブサーバーで Python スクリプトを実行する必要があります。

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

于 2012-11-15T15:09:27.187 に答える
0

Pythonスクリプトを適切にインデントしたと仮定します。また、Web サーバー環境が既に構成されていることも前提としています。それ以外にも

それ以外の

return data

絶対です

print data

あなたのpythonスクリプトで。$.getJSONはスクリプトからの出力を中継するため、返される変数の内容は中継しません。

于 2012-11-15T15:10:43.910 に答える