tmaster
tmaster

Reputation: 9655

how to Read JSON object using ajax from a python wsgi application

So here is my python application.

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()

So Now all I want is to get the return value to display on the web. I read there is function jQuery.getJSON. but I don't quite understand how it works. http://api.jquery.com/jQuery.getJSON/

Thanks.

Upvotes: 1

Views: 2263

Answers (1)

ukessi
ukessi

Reputation: 1391

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

If your jQuery version < 1.5

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

PS: if you return a json object, you'd better set the Content-Type to application/json

Upvotes: 2

Related Questions