1

I have the following simple code but getjson somehow doesnt work. My python script prints a json encoded value properly. Can anyone suggest anything?

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

2 Answers 2

2

you can't run a python script from Jquery.

you need to have your python script running with a webserver to serve the response.

starting point : http://docs.python.org/2/howto/webservers.html

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

Comments

0

I'll assume that you indented your python script properly. I'll assume also that you already have a webserver environment configured. Besides that

Instead of

return data

you must

print data

In your python script. Since $.getJSON will relay the output from your script and not the content of a variable you are returning.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.