I've searched high and low for an answer and can't figure out what i'm doing wrong. I'm creating an api that returns json data. I would prefer the response to be printed to the browser as well, if possible. What am I doing wrong?
#!/usr/bin/python
import simplejson as json
class serve_json:
def __init__(self):
x = {"msg": "Some message", "error": "Some error"}
html_to_display = 'Content-Type: application/json\n\n'
html_to_display += json.dumps(x)
print html_to_display
serve_json()
The above code doesn't work, and it doesn't print the result to the browser. If I change the Content-Type to "text/html", it prints to the screen fine, but still doesn't work as json data.
- I'm not using any framework, everything i'm doing is straight python/javascript.
- This script is executed by an http POST request.
This script is executed via /cgi
http://grouped.com/cgi-bin/upload_example.php (Works perfectly)
- http://grouped.com/cgi-bin/upload_example.py (Does not work - Content-Type = text/html)
- The code listed above also does not work either and represents an identical example as the one above except the Content-Type is set to application/json