Twisted.Web and AJAX I've encountered a similar thread where the issue of fetching data with AJAX from a Twisted server is discussed. Despite trying the code provided in that thread, I still face the same problem - the Twisted server works perfectly, but I can't seem to fetch data with ajax. In the other thread, it was mentioned that an alert pops up without any data. However, in my case, the alert doesn't even show up. Other ajax functions work fine, indicating that the issue lies specifically with fetching data. It's worth noting that I am able to fetch data using curl -
$ curl --url http://localhost:8082/test -v
- which displays 'hello world', confirming that the server is functioning correctly.
Any suggestions on how to resolve this issue?
<script type="text/javascript">
// Submit button
$(function(){
$.ajax({type: "POST",
$('a').click(function(){
url: "http://localhost:8082/test",
data: {},
success: function(data) {alert("Success:" + data);}
});
});
});
</script>
<html>
[...]
<a href="#">Load Favorites Movies</a>...
[...]
</html>
server.py
from twisted.web import server, resource, http
class RootResource(resource.Resource):
def __init__(self):
resource.Resource.__init__(self)
self.putChild('test', TestHandler())
class TestHandler(resource.Resource):
isLeaf = True
def __init__(self):
resource.Resource.__init__(self)
def render_GET(self, request):
return self.render_POST(request)
def render_POST(self, request):
return "hello world!"
if __name__ == "__main__":
import sys
from twisted.internet import reactor
reactor.listenTCP(8082, server.Site(RootResource()))
reactor.run()
A special thank you to Peter Le Bek and Darkporter. Peter Le Bek's answer was very helpful and Darkporter's vote up is greatly appreciated! =)
Answer: Peter's solution worked perfectly once I realized the importance of specifying the static folder path. Just provide any folder, place an index.html file inside, and it will serve as the root directory when accessed via web browser.