2012-11-15 2 views
1

다음과 같은 간단한 코드가 있지만 getjson이 어떻게 작동합니까? 파이썬 스크립트가 json으로 인코딩 된 값을 올바르게 인쇄합니다. 누구든지 추천 할 수 있습니까?파이썬 스크립트를 호출 할 때 getJson은 작동하지 않습니다.

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 

답변

2

당신이 JQuery와에서 파이썬 스크립트를 실행할 수 없습니다.

응답을 제공하려면 웹 서버에서 실행중인 python 스크립트가 있어야합니다.

시작 지점 : http://docs.python.org/2/howto/webservers.html

+1

감사합니다. 웹 서버의 예 또는 어떻게 구현할 수 있습니까? – Ataman

+0

http://docs.python.org/2/howto/webservers.html 사용해보기 – Jharwood

0

난 당신이 제대로 파이썬 스크립트를 들여이라고 가정합니다. 또한 웹 서버 환경이 이미 구성되어 있다고 가정합니다. 대신

return data 

게다가 당신이 당신의 파이썬 스크립트에서해야

print data 

. $.getJSON은 반환 할 변수의 내용이 아니라 스크립트의 출력을 중계합니다.

관련 문제