I have an Ajax function which is able to post json code to my python CGI script. But the cgi script is not able to read the json.
Ajax implementation:-
$.ajax({
url: "add.py",
type: "POST",
data: JSON.stringify({"things":data}),
success: function(response){
alert(response)
//var res = JSON.parse(response);
}
});
When i see the headers in add.py i see a json.
{"things":[{"name":"jnj","qty":23}]}
But when my python cgi script is not able to read this json.
I get the below error(taken from apache log file).
Original exception was:
Traceback (most recent call last):
File "/var/www/things/add.py", line 27, in <module>
myjson = json.load(sys.stdin)
File "/usr/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[Fri Jul 12 01:53:24.965064 2019] [http:error] [pid 444:tid 139764057495296] [client 172.17.0.1:47902] AH02429: Response header name '<!--' contains invalid characters, aborting request, referer: http://localhost:8008/things/add.html
which basically states that the object is None.
Below is my python code, can you tell me what is wrong in it. I have used both json.loads & json.load module to load json, but no luck.
#!/usr/bin/python3
import cgi
import cgitb
import json
import sys
import os
cgitb.enable()
form = cgi.FieldStorage()
myjson = json.load(sys.stdin)
print("Content-type: application/json\n\n")
print('<html>')
print('<head>')
print("<p>hello")
print("<p>",json.dumps(myjson),"</p>")
print(' </head>')
print('</html>')