如何使用python wsgi应用程序中的ajax读取JSON对象

所以这是我的python应用程序。

from wsgiref.simple_server import make_server import json def application(environ, start_response): result = [{"name":"John Johnson","street":"Oslo West 555","age":33}] response_body = json.dumps(result) status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body] httpd = make_server('localhost',8051,application) httpd.handle_request() 

所以现在我想要的是获得要在网络上显示的返回值。 我读到有函数jQuery.getJSON。 但我不太明白它是如何工作的。 http://api.jquery.com/jQuery.getJSON/

谢谢。

 $.getJSON('http://localhost:8051').success(function(data) { //here `data` will be your `result` in your python application }); 

如果您的jQuery版本<1.5

 $.getJSON('http://localhost:8051', function(data){ // data == result }); 

PS:如果你返回一个json对象,你最好将Content-Type设置为application/json