An ajax request is made using the "POST" method with these parameters:
function sendDataToServer(portNumber){
console.log(portNumber)
$.ajax({url: "action",
dataType : 'html',
type: "POST",
data: portNumber,
success: function(responseData){;
console.log(responseData);
$("#content3").html(responseData);
}
});
};
The ajax request is handled by a twisted python script (see relevant code below):
class Test3Handler(resource.Resource):
isLeaf = True
def __init__(self):
resource.Resource.__init__(self)
def render_POST(self, request):
dataReceived = request.content.getvalue()
print(dataReceived)
response = "<ul><li>"
response += dataReceived
response += "</ul>"
print (response)
return response
if __name__ == "__main__":
import sys
from twisted.internet import reactor
testHandler = TestHandler()
test2Handler = Test2Handler()
test3Handler = Test3Handler()
root = static.File('/home/pi/web4')
root.putChild('test', testHandler)
root.putChild('test2', test2Handler)
root.putChild('action', test3Handler)
reactor.listenTCP(8082, server.Site(root))
reactor.run()
The issue lies in not receiving the "data" parameter sent by Ajax (data: portNumber). The variable "dataReceived" appears to be empty.
As I am new to Python/Ajax, any help in resolving this issue would be greatly appreciated. This will pave the way for more complex development tasks in the future. Thank you! - Gilles