With JS, i send a AJAX post request.
$.ajax(
{method:"POST",
url:"https://my/website/send_data.py",
data:JSON.stringify(data),
contentType: 'application/json;charset=UTF-8'
On my Apache2 mod_Python server, I wish for my python file to access data. How can i do this?
def index(req):
# data = ??
PS: here is how to reproduce the problem. Create testjson.html:
<script type="text/javascript">
xhr = new XMLHttpRequest();
xhr.open("POST", "testjson.py");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function(res) { console.log(xhr.responseText); };
xhr.send(JSON.stringify({'foo': '0', 'bar': '1'}));
</script>
and create testjson.py containing:
from mod_python import apache
def index(req):
req.content_type = "application/json"
req.write("hello")
data = req.read()
return apache.OK
Create a .htaccess containing:
AddHandler mod_python .py
PythonHandler mod_python.publisher
Here is the result:
testjson.html:10 POST http://localhost/test_py/testjson.py 501 (Not Implemented)


As pointed by Grisha (mod_python's author) in a private communication, here is the reason why
application/jsonis not supported and outputs a "HTTP 501 Not implemented" error:https://github.com/grisha/mod_python/blob/master/lib/python/mod_python/util.py#L284
The solution is either to modify this, or to use a regular
application/x-www-form-urlencodedencoding, or to use something else than themod_python.publisherhandler.Example with
mod_pythonandPythonHandler mod_python.publisher:Server-side:
Output:
Success!